<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width,initial-scale=1"><title>node.js学习总结 | mozihao</title><meta name="keywords" content="node.js"><meta name="author" content="mozihao"><meta name="copyright" content="mozihao"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta name="description" content="CommomJS规范: 外部的引用exports和module.exports终端创建文件: touch 文件名  exports&#x2F;&#x2F;dome.js 123456789101112console.log(&quot;dome&quot;);exports.hello &#x3D; &quot;hello&quot;;exports.add &#x3D; function(a, b) &amp;#123;    return">
<meta property="og:type" content="article">
<meta property="og:title" content="node.js学习总结">
<meta property="og:url" content="https://mo_zi_hao.gitee.io/blog/posts/25418/index.html">
<meta property="og:site_name" content="mozihao">
<meta property="og:description" content="CommomJS规范: 外部的引用exports和module.exports终端创建文件: touch 文件名  exports&#x2F;&#x2F;dome.js 123456789101112console.log(&quot;dome&quot;);exports.hello &#x3D; &quot;hello&quot;;exports.add &#x3D; function(a, b) &amp;#123;    return">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://gitee.com/mo_zi_hao/picsBed/raw/master/image/%E8%8B%B1%E8%AF%AD%E5%AD%A6%E4%B9%A0%E8%8B%B1%E6%A0%87%E5%A4%A7%E5%85%A8.jpg">
<meta property="article:published_time" content="2020-06-08T06:18:57.000Z">
<meta property="article:modified_time" content="2021-08-29T15:22:14.597Z">
<meta property="article:author" content="mozihao">
<meta property="article:tag" content="node.js">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://gitee.com/mo_zi_hao/picsBed/raw/master/image/%E8%8B%B1%E8%AF%AD%E5%AD%A6%E4%B9%A0%E8%8B%B1%E6%A0%87%E5%A4%A7%E5%85%A8.jpg"><link rel="shortcut icon" href="/blog/img/favicon.png"><link rel="canonical" href="https://mo_zi_hao.gitee.io/blog/posts/25418/"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><link rel="stylesheet" href="/blog/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free/css/all.min.css" media="print" onload="this.media='all'"><script>const GLOBAL_CONFIG = { 
  root: '/blog/',
  algolia: undefined,
  localSearch: {"path":"search.xml","languages":{"hits_empty":"找不到您查询的内容：${query}"}},
  translate: {"defaultEncoding":2,"translateDelay":0,"msgToTraditionalChinese":"繁","msgToSimplifiedChinese":"簡"},
  noticeOutdate: undefined,
  highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":false},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: false,
    post: false
  },
  runtime: '',
  date_suffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: undefined,
  lightbox: 'fancybox',
  Snackbar: undefined,
  source: {
    jQuery: 'https://cdn.jsdelivr.net/npm/jquery@latest/dist/jquery.min.js',
    justifiedGallery: {
      js: 'https://cdn.jsdelivr.net/npm/justifiedGallery/dist/js/jquery.justifiedGallery.min.js',
      css: 'https://cdn.jsdelivr.net/npm/justifiedGallery/dist/css/justifiedGallery.min.css'
    },
    fancybox: {
      js: 'https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@latest/dist/jquery.fancybox.min.js',
      css: 'https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@latest/dist/jquery.fancybox.min.css'
    }
  },
  isPhotoFigcaption: false,
  islazyload: true,
  isanchor: false
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = { 
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: true,
  postUpdate: '2021-08-29 23:22:14'
}</script><noscript><style type="text/css">
  #nav {
    opacity: 1
  }
  .justified-gallery img {
    opacity: 1
  }

  #recent-posts time,
  #post-meta time {
    display: inline !important
  }
</style></noscript><script>(win=>{
    win.saveToLocal = {
      set: function setWithExpiry(key, value, ttl) {
        if (ttl === 0) return
        const now = new Date()
        const expiryDay = ttl * 86400000
        const item = {
          value: value,
          expiry: now.getTime() + expiryDay,
        }
        localStorage.setItem(key, JSON.stringify(item))
      },

      get: function getWithExpiry(key) {
        const itemStr = localStorage.getItem(key)

        if (!itemStr) {
          return undefined
        }
        const item = JSON.parse(itemStr)
        const now = new Date()

        if (now.getTime() > item.expiry) {
          localStorage.removeItem(key)
          return undefined
        }
        return item.value
      }
    }
  
    win.getScript = url => new Promise((resolve, reject) => {
      const script = document.createElement('script')
      script.src = url
      script.async = true
      script.onerror = reject
      script.onload = script.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        script.onload = script.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(script)
    })
  
      win.activateDarkMode = function () {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = function () {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
          if (t === 'dark') activateDarkMode()
          else if (t === 'light') activateLightMode()
        
      const asideStatus = saveToLocal.get('aside-status')
      if (asideStatus !== undefined) {
        if (asideStatus === 'hide') {
          document.documentElement.classList.add('hide-aside')
        } else {
          document.documentElement.classList.remove('hide-aside')
        }
      }
    })(window)</script><link rel="stylesheet" type="text/css" href="./css/iconfont.css" media="all"><meta name="generator" content="Hexo 5.4.2"><link rel="alternate" href="/blog/atom.xml" title="mozihao" type="application/atom+xml">
</head><body><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="author-avatar"><img class="avatar-img" src="https://gitee.com/mo_zi_hao/picsBed/raw/master/image/%E6%B5%B7%E8%B4%BC008.jpg" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="site-data"><div class="data-item is-center"><div class="data-item-link"><a href="/blog/archives/"><div class="headline">文章</div><div class="length-num">24</div></a></div></div><div class="data-item is-center"><div class="data-item-link"><a href="/blog/tags/"><div class="headline">标签</div><div class="length-num">16</div></a></div></div><div class="data-item is-center"><div class="data-item-link"><a href="/blog/categories/"><div class="headline">分类</div><div class="length-num">11</div></a></div></div></div><hr/><div class="menus_items"><div class="menus_item"><a class="site-page" href="/blog/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/blog/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page" href="/blog/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/blog/archives/"><i class="fa-fw fas fa-archive"></i><span> 归档</span></a></div><div class="menus_item"><a class="site-page" href="/blog/about/"><i class="fa-fw fas fa-heart"></i><span> 前端知识思维导图</span></a></div></div></div></div><div class="post" id="body-wrap"><header class="post-bg" id="page-header" style="background-image: url('https://gitee.com/mo_zi_hao/picsBed/raw/master/image/%E8%8B%B1%E8%AF%AD%E5%AD%A6%E4%B9%A0%E8%8B%B1%E6%A0%87%E5%A4%A7%E5%85%A8.jpg')"><nav id="nav"><span id="blog_name"><a id="site-name" href="/blog/">mozihao</a></span><div id="menus"><div id="search-button"><a class="site-page social-icon search"><i class="fas fa-search fa-fw"></i><span> 搜索</span></a></div><div class="menus_items"><div class="menus_item"><a class="site-page" href="/blog/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/blog/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page" href="/blog/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/blog/archives/"><i class="fa-fw fas fa-archive"></i><span> 归档</span></a></div><div class="menus_item"><a class="site-page" href="/blog/about/"><i class="fa-fw fas fa-heart"></i><span> 前端知识思维导图</span></a></div></div><div id="toggle-menu"><a class="site-page"><i class="fas fa-bars fa-fw"></i></a></div></div></nav><div id="post-info"><h1 class="post-title">node.js学习总结</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2020-06-08T06:18:57.000Z" title="发表于 2020-06-08 14:18:57">2020-06-08</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2021-08-29T15:22:14.597Z" title="更新于 2021-08-29 23:22:14">2021-08-29</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/blog/categories/node/">node</a></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-wordcount"><i class="far fa-file-word fa-fw post-meta-icon"></i><span class="post-meta-label">字数总计:</span><span class="word-count">7.7k</span><span class="post-meta-separator">|</span><i class="far fa-clock fa-fw post-meta-icon"></i><span class="post-meta-label">阅读时长:</span><span>35分钟</span></span><span class="post-meta-separator">|</span><span class="post-meta-pv-cv" id="" data-flag-title="node.js学习总结"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">阅读量:</span><span id="busuanzi_value_page_pv"></span></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><h3 id="CommomJS规范-外部的引用exports和module-exports"><a href="#CommomJS规范-外部的引用exports和module-exports" class="headerlink" title="CommomJS规范: 外部的引用exports和module.exports"></a>CommomJS规范: 外部的引用exports和module.exports</h3><p>终端创建文件: touch 文件名</p>
<p><img src="https://gitee.com/mo_zi_hao/picsBed/raw/master/image/commonjs.png" alt="commonjs"></p>
<h4 id="exports"><a href="#exports" class="headerlink" title="exports"></a>exports</h4><p>//dome.js</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">console</span>.log(<span class="string">&quot;dome&quot;</span>);</span><br><span class="line"><span class="built_in">exports</span>.hello = <span class="string">&quot;hello&quot;</span>;</span><br><span class="line"><span class="built_in">exports</span>.add = <span class="function"><span class="keyword">function</span>(<span class="params">a, b</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">return</span> a + b;</span><br><span class="line">&#125;;</span><br><span class="line"><span class="built_in">exports</span>.person = &#123;</span><br><span class="line">    name: <span class="string">&quot;小明&quot;</span></span><br><span class="line">&#125;;</span><br><span class="line"><span class="built_in">setTimeout</span>(<span class="function">() =&gt;</span> &#123;</span><br><span class="line">    <span class="comment">// 这里能打印出外部引用添加的exports的变量</span></span><br><span class="line">    <span class="built_in">console</span>.log(<span class="built_in">exports</span>);</span><br><span class="line">&#125;, <span class="number">2000</span>);</span><br></pre></td></tr></table></figure>

<p>//index.js</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">console</span>.log(<span class="string">&quot;start&quot;</span>);</span><br><span class="line"><span class="keyword">var</span> dome = <span class="built_in">require</span>(<span class="string">&#x27;./dome.js&#x27;</span>)</span><br><span class="line"><span class="built_in">console</span>.log(<span class="string">&quot;end&quot;</span>,dome);</span><br><span class="line"><span class="comment">// 同一个引用外部可以改变exports导出的内容</span></span><br><span class="line">dome.additional = <span class="string">&quot;test&quot;</span></span><br></pre></td></tr></table></figure>

<p>终端执行 node index.js 打印结果为</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">start</span><br><span class="line">dome</span><br><span class="line">end &#123; hello: &#39;hello&#39;, add: [Function], person: &#123; name: &#39;小明&#39; &#125; &#125;</span><br></pre></td></tr></table></figure>

<p>//两秒后打印所有的exports的变量</p>
<figure class="highlight"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">&#123;</span><br><span class="line">hello: &#x27;hello&#x27;,</span><br><span class="line">add: [Function],</span><br><span class="line">person: &#123; name: &#x27;小明&#x27; &#125;,</span><br><span class="line">additional: &#x27;test&#x27;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<h4 id="module-exports"><a href="#module-exports" class="headerlink" title="module.exports"></a>module.exports</h4><p>//index.js<br>代码不变</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">console</span>.log(<span class="string">&quot;start&quot;</span>);</span><br><span class="line"><span class="keyword">var</span> dome = <span class="built_in">require</span>(<span class="string">&#x27;./dome.js&#x27;</span>)</span><br><span class="line"><span class="built_in">console</span>.log(<span class="string">&quot;end&quot;</span>,dome);</span><br><span class="line"><span class="comment">// 同一个引用外部可以改变exports导出的内容</span></span><br><span class="line">dome.additional = <span class="string">&quot;test&quot;</span></span><br></pre></td></tr></table></figure>

<p>//dome.js 加入module.exports的应用</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">console</span>.log(<span class="string">&quot;dome&quot;</span>);</span><br><span class="line"><span class="built_in">exports</span>.hello = <span class="string">&quot;hello&quot;</span>;</span><br><span class="line"><span class="built_in">exports</span>.add = <span class="function"><span class="keyword">function</span>(<span class="params">a, b</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">return</span> a + b;</span><br><span class="line">&#125;;</span><br><span class="line"><span class="built_in">exports</span>.person = &#123;</span><br><span class="line">    name: <span class="string">&quot;小明&quot;</span></span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">// 外部的引用会被这个module.exports取代 会覆盖所有的exports</span></span><br><span class="line"><span class="built_in">module</span>.exports = <span class="function"><span class="keyword">function</span> <span class="title">minus</span>(<span class="params">a, b</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">return</span> a - b;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="built_in">setTimeout</span>(<span class="function">() =&gt;</span> &#123;</span><br><span class="line">    <span class="comment">// 这里能打印出外部引用添加的exports的变量</span></span><br><span class="line">    <span class="built_in">console</span>.log(<span class="built_in">exports</span>);</span><br><span class="line">&#125;, <span class="number">2000</span>);</span><br></pre></td></tr></table></figure>

<p>终端执行 node index.js 打印结果为</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">start</span><br><span class="line">dome</span><br><span class="line">end <span class="function"><span class="keyword">function</span> <span class="title">minus</span>(<span class="params">a, b</span>) </span>&#123;</span><br><span class="line"><span class="keyword">return</span> a - b;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>


<p>//两秒后打印所有的exports的变量<br>{ hello: ‘hello’, add: [Function], person: { name: ‘小明’ } }</p>
<h4 id="显然外部的引用会被这个module-exports取代-会覆盖所有的exports"><a href="#显然外部的引用会被这个module-exports取代-会覆盖所有的exports" class="headerlink" title="显然外部的引用会被这个module.exports取代,会覆盖所有的exports"></a>显然外部的引用会被这个module.exports取代,会覆盖所有的exports</h4><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//webpack打包代码// webpack --devtool none --mode development --target node </span></span><br></pre></td></tr></table></figure>



<h3 id="简单的石头剪刀布游戏"><a href="#简单的石头剪刀布游戏" class="headerlink" title="简单的石头剪刀布游戏"></a>简单的石头剪刀布游戏</h3><p><img src="https://gitee.com/mo_zi_hao/picsBed/raw/master/image/%E5%89%AA%E5%88%80%E7%9F%B3%E5%A4%B4%E5%B8%83%E6%B8%B8%E6%88%8F.png" alt="剪刀石头布游戏"></p>
<p>//获取到程序运行时,在终端输入的字符,因为输入的字符为process.argv数组的最后一个,此处你可以打印一下,这边就不进行演示了<br>process.argv[process.argv.length - 1];</p>
<h4 id="获取输入的方法还有就是使用进程控制台输入流-process-stdin-下面使用进程控制台输入流的来获取我们在终端输入的字符"><a href="#获取输入的方法还有就是使用进程控制台输入流-process-stdin-下面使用进程控制台输入流的来获取我们在终端输入的字符" class="headerlink" title="获取输入的方法还有就是使用进程控制台输入流, process.stdin,下面使用进程控制台输入流的来获取我们在终端输入的字符"></a>获取输入的方法还有就是使用进程控制台输入流, process.stdin,下面使用进程控制台输入流的来获取我们在终端输入的字符</h4><p>下面讲一下游戏代码逻辑,就是电脑先随机生成一个0到3的随机数,判断如果小于0为石头,大于1小于2为剪刀,否则为布,然后通过条件判断三种赢得情况,否则为输</p>
<p>//game.js</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 使用common.js规范,易于维护--模块化</span></span><br><span class="line"><span class="built_in">module</span>.exports = <span class="function"><span class="keyword">function</span> (<span class="params">playerAction</span>) </span>&#123;</span><br><span class="line">    <span class="comment">// 终端创建文件: touch 文件名</span></span><br><span class="line">    <span class="comment">// 获取输入的字符,为数组的最后一个</span></span><br><span class="line">    <span class="comment">// exports可以挂任何东西,使用require引用文件获取文件</span></span><br><span class="line">    <span class="comment">// var playerAction = process.argv[process.argv.length - 1];</span></span><br><span class="line">    <span class="built_in">console</span>.log(<span class="string">&quot;你出:&quot;</span> + playerAction);</span><br><span class="line">    <span class="keyword">var</span> random = <span class="built_in">Math</span>.random() * <span class="number">3</span>;</span><br><span class="line">    <span class="keyword">if</span> (random &lt; <span class="number">1</span>) &#123;</span><br><span class="line">        <span class="keyword">var</span> computerAction = <span class="string">&quot;rock&quot;</span>;</span><br><span class="line">    &#125; <span class="keyword">else</span> <span class="keyword">if</span> (<span class="number">1</span> &lt; random &amp;&amp; random &gt; <span class="number">2</span>) &#123;</span><br><span class="line">        <span class="keyword">var</span> computerAction = <span class="string">&quot;scissor&quot;</span>;</span><br><span class="line">    &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">        <span class="keyword">var</span> computerAction = <span class="string">&quot;paper&quot;</span>;</span><br><span class="line">&#125;</span><br><span class="line"><span class="built_in">console</span>.log(<span class="string">&quot;电脑出:&quot;</span> + computerAction)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    <span class="keyword">if</span> (computerAction == playerAction) &#123;</span><br><span class="line">        <span class="built_in">console</span>.log(<span class="string">&quot;平局&quot;</span>);</span><br><span class="line">        <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">    &#125; <span class="keyword">else</span> <span class="keyword">if</span> (</span><br><span class="line">        (computerAction === <span class="string">&quot;rock&quot;</span> &amp;&amp; playerAction === <span class="string">&quot;paper&quot;</span>) ||</span><br><span class="line">        (computerAction === <span class="string">&quot;paper&quot;</span> &amp;&amp; playerAction === <span class="string">&quot;scissor&quot;</span>) ||</span><br><span class="line">        (computerAction === <span class="string">&quot;scissor&quot;</span> &amp;&amp; playerAction === <span class="string">&quot;rock&quot;</span>)</span><br><span class="line">    ) &#123;</span><br><span class="line">        <span class="built_in">console</span>.log(<span class="string">&quot;你赢了&quot;</span>);</span><br><span class="line">        <span class="keyword">return</span> -<span class="number">1</span>;</span><br><span class="line">    &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">        <span class="built_in">console</span>.log(<span class="string">&quot;你输了&quot;</span>);</span><br><span class="line">        <span class="keyword">return</span> <span class="number">1</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>

<h4 id="通过进程控制台输入流获取在终端输入的数据process-stdin获取输入的数据"><a href="#通过进程控制台输入流获取在终端输入的数据process-stdin获取输入的数据" class="headerlink" title="通过进程控制台输入流获取在终端输入的数据process.stdin获取输入的数据"></a>通过进程控制台输入流获取在终端输入的数据process.stdin获取输入的数据</h4><p>//index.js</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//var playerAction = process.argv[process.argv.length - 1];</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> game = <span class="built_in">require</span>(<span class="string">&quot;./game&quot;</span>);</span><br><span class="line"><span class="comment">// game(playerAction)</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">let</span> count = <span class="number">0</span>;</span><br><span class="line">process.stdin.on(<span class="string">&quot;data&quot;</span>, <span class="function"><span class="params">e</span>=&gt;</span> &#123;</span><br><span class="line">    <span class="keyword">const</span> playerAction = e.toString().trim()</span><br><span class="line">    <span class="keyword">const</span> result = game(playerAction);</span><br><span class="line">    <span class="keyword">if</span>(result == -<span class="number">1</span>)&#123;</span><br><span class="line">        count++;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">if</span>(count === <span class="number">3</span>)&#123;</span><br><span class="line">        <span class="built_in">console</span>.log(<span class="string">&quot;好了好了,你赢了&quot;</span>);</span><br><span class="line">        <span class="comment">// 退出程序</span></span><br><span class="line">        process.exit();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure>

<p>终端执行 node index.js 运行并输入（此处输入rock为例） 打印结果为</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line">rock</span><br><span class="line">你出：rock</span><br><span class="line">电脑出：scissor</span><br><span class="line">你赢了</span><br><span class="line">rock</span><br><span class="line">你出：rock</span><br><span class="line">电脑出：scissor</span><br><span class="line">你赢了</span><br><span class="line">rock</span><br><span class="line">你出：rock</span><br><span class="line">电脑出：rock</span><br><span class="line">平局</span><br><span class="line">rock</span><br><span class="line">你出：rock</span><br><span class="line">电脑出：scissor</span><br><span class="line">你赢了</span><br><span class="line">好了好了,你赢了</span><br></pre></td></tr></table></figure>



<h3 id="node-js-API解读-process"><a href="#node-js-API解读-process" class="headerlink" title="node.js(API解读) - process"></a>node.js(API解读) - process</h3><h4 id="Event-‘exit’"><a href="#Event-‘exit’" class="headerlink" title="Event: ‘exit’"></a>Event: ‘exit’</h4><p>这是process的退出事件，官方示例很清楚，当process退出时触发。即我们按“ctrl+c”时触发。</p>
<h4 id="Event-‘uncaughtException’"><a href="#Event-‘uncaughtException’" class="headerlink" title="Event: ‘uncaughtException’"></a>Event: ‘uncaughtException’</h4><p>这是process的异常事件，uncaughtException译为：未捕获的异常，可以利用这个函数来捕获整个进程运行时的异常，这里可以理解为“使本node.js进程中断的异常”，因为它的官方示例在异常函数下面的代码将不会执行。</p>
<h4 id="Signal-Events"><a href="#Signal-Events" class="headerlink" title="Signal Events"></a>Signal Events</h4><p>这是process的自定义事件，可以为process对象自定义一些事件。这里官方示例如下：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">process.stdin.resume(); &#x2F;&#x2F;这句话是为了不让控制台推出</span><br><span class="line">process.on(&#39;SIGINT&#39;, function () &#123; &#x2F;&#x2F;SIGINT这个信号是系统默认信号，代表信号中断，就是ctrl+c</span><br><span class="line">console.log(&#39;Got SIGINT.  Press Control-D to exit.&#39;);</span><br><span class="line">&#125;);</span><br><span class="line"></span><br><span class="line">process.on(&#39;SIGUSR1&#39;, function (d) &#123; &#x2F;&#x2F;这里监听 SIGUSR1 事件</span><br><span class="line">  console.log(&#39;Bye-&#39;+d); &#x2F;&#x2F;这里将输出Bye-Bye,然后推出进程</span><br><span class="line">  process.exit(0);</span><br><span class="line">&#125;);</span><br><span class="line"> process.emit(&#39;SIGUSR1&#39;, &#39;Bye&#39;); &#x2F;&#x2F;利用emit触发SIGUSR1，然后传参数为Bye</span><br></pre></td></tr></table></figure>

<h4 id="process-stdout"><a href="#process-stdout" class="headerlink" title="process.stdout"></a>process.stdout</h4><p>官方的示例和代码很清楚，控制台输出流。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">console.log &#x3D; function (d) &#123;</span><br><span class="line">  process.stdout.write(d + &#39;\n&#39;);</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>

<h4 id="process-stdin"><a href="#process-stdin" class="headerlink" title="process.stdin"></a>process.stdin</h4><p>进程控制台输入流，官方代码看了不解，于是copy到linux系统里运行一下，就明白了</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">process.stdin.resume();</span><br><span class="line">process.stdin.setEncoding(&#39;utf8&#39;);</span><br><span class="line">process.stdin.on(&#39;data&#39;, function (chunk) &#123;</span><br><span class="line">  process.stdout.write(&#39;data: &#39; + chunk);</span><br><span class="line">&#125;);</span><br><span class="line">process.stdin.on(&#39;end&#39;, function () &#123;</span><br><span class="line">  process.stdout.write(&#39;end&#39;);</span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure>

<p>输入的命令和输出:</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">$ node test.js </span><br><span class="line">ffff</span><br><span class="line">data: ffff</span><br></pre></td></tr></table></figure>

<h4 id="process-argv"><a href="#process-argv" class="headerlink" title="process.argv"></a>process.argv</h4><p>这是一个数组，数组里存放着启动这个node.js进程各个参数和命令代码，官方代码一目了然。</p>
<h4 id="process-execPath"><a href="#process-execPath" class="headerlink" title="process.execPath"></a>process.execPath</h4><p>返回当前node.js进程的启动命令路径，也就是node.js安装目录下的node命令路径，是一个绝对路径</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">$ node test.js </span><br><span class="line">url: &#x2F;usr&#x2F;local&#x2F;bin&#x2F;node</span><br></pre></td></tr></table></figure>

<h4 id="process-chdir-directory-和-process-cwd"><a href="#process-chdir-directory-和-process-cwd" class="headerlink" title="process.chdir(directory) 和 process.cwd()"></a>process.chdir(directory) 和 process.cwd()</h4><p>process.cwd() 返回当前进程的工作目录，process.chdir(directory)则改变进程的工作目录，如果改变失败将抛出一个异常。代码见官方API。<br>举个例子：<br>如果一个进程工作在 /red-hat/，现在创建在 “./“目录下创建一个foo.txt，则会在/red-hat/foo.txt创建，如果改变进程工作目录为/red/，则创建foo.txt会在/red/foo.txt创建。注意：node.js的require()不受这个限制,他是以当前文件的。在执行child_process.exec()方法时需要考虑这一点。</p>
<h4 id="process-env"><a href="#process-env" class="headerlink" title="process.env"></a>process.env</h4><p>返回当前linux系统的信息，我可以输入一下代码来看系统信息</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">console.log(JSON.stringify(process.env));</span><br></pre></td></tr></table></figure>

<h4 id="process-exit-code-0"><a href="#process-exit-code-0" class="headerlink" title="process.exit(code=0)"></a>process.exit(code=0)</h4><p>kill当前进程，退出本进程。</p>
<h4 id="process-getgid-、process-setgid-id-、process-getuid-、process-setuid-id"><a href="#process-getgid-、process-setgid-id-、process-getuid-、process-setuid-id" class="headerlink" title="process.getgid()、process.setgid(id)、process.getuid()、process.setuid(id)"></a>process.getgid()、process.setgid(id)、process.getuid()、process.setuid(id)</h4><p>获取和设置进程的groupid和userid。</p>
<h4 id="process-version、process-versions"><a href="#process-version、process-versions" class="headerlink" title="process.version、process.versions"></a>process.version、process.versions</h4><p>node.js的版本和node.js的版本对象</p>
<h4 id="process-installPrefix"><a href="#process-installPrefix" class="headerlink" title="process.installPrefix"></a>process.installPrefix</h4><p>返回nodejs的安装前缀</p>
<h4 id="process-kill-pid-signal-’SIGTERM’"><a href="#process-kill-pid-signal-’SIGTERM’" class="headerlink" title="process.kill(pid, signal=’SIGTERM’)"></a>process.kill(pid, signal=’SIGTERM’)</h4><p>发出一个kill信号给指定pid，如果signal不指定，则默认为“SIGTERM”，官方API特别提醒，这个kill信号只是一个信号，并不是linux下的kill命令，并不会真的将那个pid杀死，要想通过kill杀死指定pid，则需要在指定pid监听“SIGTERM”信号，然后执行process.exit(0)；即可。</p>
<h4 id="process-pid、process-title、process-arch、process-platform"><a href="#process-pid、process-title、process-arch、process-platform" class="headerlink" title="process.pid、process.title、process.arch、process.platform"></a>process.pid、process.title、process.arch、process.platform</h4><p>进程id，进程名字，进程架构（如：X64），进程平台（如：linux）</p>
<h4 id="process-memoryUsage"><a href="#process-memoryUsage" class="headerlink" title="process.memoryUsage()"></a>process.memoryUsage()</h4><p>进程的内存使用情况，API很清楚。</p>
<h4 id="process-nextTick-callback"><a href="#process-nextTick-callback" class="headerlink" title="process.nextTick(callback)"></a>process.nextTick(callback)</h4><p>异步执行callback函数，注意，这比 “setTimeout(fn, 0) “ 要高效很多。当有一些比较耗时的操作可以用在process.nextTick(callback) 中，这样可以不阻塞整个函数执行。</p>
<h4 id="process-umask-mask"><a href="#process-umask-mask" class="headerlink" title="process.umask([mask])"></a>process.umask([mask])</h4><p>设置进程的user mask值，什么是umask值呢？<br>在linux系统有一个系统命令：$ umask，主要作用是修改系统默认的创建文件和文件夹的权限。<br>注意这句话：Returns the old mask if mask argument is given, otherwise returns the current mask.<br>当对process.umask()方法传递参数时，则返回旧的umask值，否则返回当前的umask值。<br>这里如果我们设置：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">var oldmask, newmask &#x3D; 0644;</span><br><span class="line">oldmask &#x3D; process.umask(newmask);</span><br><span class="line">console.log(&#39;Changed umask from: &#39; + oldmask.toString(8) + &#39; to &#39; + newmask.toString(8));</span><br></pre></td></tr></table></figure>

<p>输出022和0644，当umask = 022时，新建的目录权限是755，新建文件的权限是 644。具体可以参考linux umask手册。这里只是修改由本node.js进程创建的文件和目录的权限。</p>
<h4 id="process-uptime"><a href="#process-uptime" class="headerlink" title="process.uptime()"></a>process.uptime()</h4><p>官方给出结果是node进程运行的秒数</p>
<h4 id="process-reallyExit-status"><a href="#process-reallyExit-status" class="headerlink" title="process.reallyExit(status)"></a>process.reallyExit(status)</h4><p>真实推出本进程，不触发‘exit’事件</p>
<h4 id="process-kill-pid-sig"><a href="#process-kill-pid-sig" class="headerlink" title="process._kill(pid,sig)"></a>process._kill(pid,sig)</h4><p>用于给指定pid的进程发送指定信号（类似linux下的kill命令）<br>var pid=process.pid;<br>process._kill(pid,9);</p>
<h4 id="process-binding-name"><a href="#process-binding-name" class="headerlink" title="process.binding(name)"></a>process.binding(name)</h4><p>这个方法用于返回指定名称的内置模块。例如下面的代码将打印node_net模块所有的可以调用的方法或变量。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">var binding&#x3D;process.binding(&#39;net&#39;);</span><br><span class="line">console.dir(binding);</span><br></pre></td></tr></table></figure>







<h3 id="观察者模式"><a href="#观察者模式" class="headerlink" title="观察者模式"></a>观察者模式</h3><p><img src="https://gitee.com/mo_zi_hao/picsBed/raw/master/image/%E8%A7%82%E5%AF%9F%E8%80%85%E6%A8%A1%E5%BC%8F.png" alt="观察者模式"></p>
<p>EventEmitter观察者模式，可以</p>
<p>添加事件 addListener(”事件名”，回调方法)</p>
<p>移除事件 removeListener(“事件名”，回调)</p>
<p>发布事件 emit(”事件名”，参数)</p>
<p><strong>//EventEmitter.js   注册newlesson事件</strong></p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// EventEmitter 观察者模式----  事件添加 、事件移除</span></span><br><span class="line"><span class="keyword">const</span> EventEmitter = <span class="built_in">require</span>(<span class="string">&quot;events&quot;</span>).EventEmitter;</span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Geektime</span> <span class="keyword">extends</span> <span class="title">EventEmitter</span> </span>&#123;    </span><br><span class="line"><span class="function"><span class="title">constructor</span>(<span class="params"></span>)</span> &#123;        </span><br><span class="line">    <span class="built_in">super</span>();       </span><br><span class="line">    <span class="built_in">setInterval</span>(<span class="function">() =&gt;</span> &#123;       <span class="comment">//EvemtEmitter.emit(&quot;事件名&quot;,参数) </span></span><br><span class="line">        <span class="built_in">this</span>.emit(<span class="string">&quot;newlesson&quot;</span>, &#123; <span class="attr">price</span>: <span class="built_in">Math</span>.random() * <span class="number">100</span> &#125;);        </span><br><span class="line">    &#125;, <span class="number">2000</span>);</span><br><span class="line">&#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">const</span> geektime = <span class="keyword">new</span> Geektime();<span class="comment">// 使用common.js规范module.exports = geektime;</span></span><br></pre></td></tr></table></figure>



<h4 id="事件触发监听newlesson事件，进行方法回调"><a href="#事件触发监听newlesson事件，进行方法回调" class="headerlink" title="事件触发监听newlesson事件，进行方法回调"></a>事件触发监听newlesson事件，进行方法回调</h4><p><strong>//index.js</strong></p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> geektime = <span class="built_in">require</span>(<span class="string">&quot;./EventEmitter&quot;</span>);<span class="comment">// 添加监听事件,res 获取事件参数</span></span><br><span class="line">geektime.addListener(<span class="string">&quot;newlesson&quot;</span>, <span class="function"><span class="params">res</span> =&gt;</span> &#123;    </span><br><span class="line">    <span class="keyword">if</span> (res.price &lt; <span class="number">80</span>) &#123;        </span><br><span class="line">        <span class="built_in">console</span>.log(<span class="string">&quot;事件触发&quot;</span>, res);    </span><br><span class="line">    &#125; <span class="keyword">else</span> &#123;        </span><br><span class="line">        <span class="built_in">console</span>.log(<span class="string">&quot;这个超出预算不触发&quot;</span>, res);    </span><br><span class="line">    &#125;</span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure>



<p>部分打印结果为：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">事件触发 &#123; price: 79.87022256963819 &#125;</span><br><span class="line">事件触发 &#123; price: 46.60957536621373 &#125;</span><br><span class="line">这个超出预算不触发 &#123; price: 98.27772021393942 &#125;</span><br><span class="line">事件触发 &#123; price: 70.20120424421603 &#125;</span><br><span class="line">事件触发 &#123; price: 62.63782955623185 &#125;</span><br><span class="line">事件触发 &#123; price: 50.71547809292787 &#125;</span><br><span class="line">事件触发 &#123; price: 47.076875751870475 &#125;</span><br><span class="line">事件触发 &#123; price: 46.98828960282562 &#125;</span><br><span class="line">这个超出预算不触发 &#123; price: 91.61548688993928 &#125;</span><br></pre></td></tr></table></figure>



<h3 id="非阻塞I-O"><a href="#非阻塞I-O" class="headerlink" title="非阻塞I/O"></a>非阻塞I/O</h3><p>为了比较阻塞I/O和非阻塞I/O，我们通过glob插件遍历文件来说明</p>
<p><img src="https://gitee.com/mo_zi_hao/picsBed/raw/master/image/%E9%98%BB%E5%A1%9Eio1.png" alt="阻塞io1"></p>
<p>首先初始化项目 npm init<br>然后安装glob依赖 npm install glob</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">&#x2F;&#x2F; npm init </span><br><span class="line">&#x2F;&#x2F; npm install glob</span><br></pre></td></tr></table></figure>

<p>//index.js</p>
<p>阻塞I/O—-先遍历完所有文件再执行下面的打印操作console.log</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">const glob &#x3D; require(&quot;glob&quot;);</span><br><span class="line"></span><br><span class="line">&#x2F;&#x2F; 阻塞IO   使用时间27ms----时间这个是根据自己的目录下文件的数量</span><br><span class="line">var result &#x3D; null;</span><br><span class="line">console.time(&quot;glob&quot;);</span><br><span class="line">result &#x3D; glob.sync(__dirname + &quot;&#x2F;**&#x2F;*&quot;);</span><br><span class="line">console.timeEnd(&quot;glob&quot;);</span><br><span class="line">console.log(result);</span><br></pre></td></tr></table></figure>

<p>非阻塞I/O—-在遍历文件的时候，继续进行其他操作</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">&#x2F;&#x2F; 非阻塞IO  使用时5ms----减少我们在nodejs之间的等待时间,在glob的过程中也可以进行其他操作</span><br><span class="line">var result &#x3D; null;</span><br><span class="line">console.time(&quot;glob&quot;);</span><br><span class="line">glob(__dirname + &#39;&#x2F;**&#x2F;*&#39;, function(err, res) &#123;</span><br><span class="line">    result &#x3D; res;</span><br><span class="line">    console.log(&quot;got result&quot;);</span><br><span class="line">    &#x2F;&#x2F; console.log(result);</span><br><span class="line">&#125;);</span><br><span class="line">console.timeEnd(&quot;glob&quot;);</span><br><span class="line">console.log(1 + 1);</span><br></pre></td></tr></table></figure>



<p><img src="https://gitee.com/mo_zi_hao/picsBed/raw/master/image/0" alt="img"></p>
<h4 id="Node-js的所有I-O操作都是非阻塞的-会把大量的计算能力分发给其他c-线程计算等到其他c-线程把这些计算都执行完毕之后就把结果回调到我们的node-js线程-node-js再把线程把数据结果返回给我们的应用程序"><a href="#Node-js的所有I-O操作都是非阻塞的-会把大量的计算能力分发给其他c-线程计算等到其他c-线程把这些计算都执行完毕之后就把结果回调到我们的node-js线程-node-js再把线程把数据结果返回给我们的应用程序" class="headerlink" title="Node.js的所有I/O操作都是非阻塞的,会把大量的计算能力分发给其他c++线程计算等到其他c++线程把这些计算都执行完毕之后就把结果回调到我们的node.js线程,node.js再把线程把数据结果返回给我们的应用程序"></a>Node.js的所有I/O操作都是非阻塞的,会把大量的计算能力分发给其他c++线程计算等到其他c++线程把这些计算都执行完毕之后就把结果回调到我们的node.js线程,node.js再把线程把数据结果返回给我们的应用程序</h4><h3 id="Eventloop事件循环"><a href="#Eventloop事件循环" class="headerlink" title="Eventloop事件循环"></a>Eventloop事件循环</h3><p><img src="https://gitee.com/mo_zi_hao/picsBed/raw/master/image/12306.png"></p>
<p>//index.js<br>//模拟事件循环，如果quene队列里面存在事件，则出队执行，定义一个add方法添加事件到队列中</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 事件循环</span></span><br><span class="line"><span class="keyword">const</span> eventloop = &#123;</span><br><span class="line">    queue: [],</span><br><span class="line">    <span class="function"><span class="title">loop</span>(<span class="params"></span>)</span> &#123;</span><br><span class="line">        <span class="keyword">while</span> (<span class="built_in">this</span>.queue.length) &#123;</span><br><span class="line">            <span class="keyword">var</span> callback = <span class="built_in">this</span>.queue.shift();</span><br><span class="line">            callback();</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="built_in">setTimeout</span>(<span class="built_in">this</span>.loop.bind(<span class="built_in">this</span>), <span class="number">500</span>);</span><br><span class="line">    &#125;,</span><br><span class="line">    <span class="function"><span class="title">add</span>(<span class="params">callback</span>)</span> &#123;</span><br><span class="line">        <span class="built_in">this</span>.queue.push(callback);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>

<p>//执行队列里面的事件，然后800毫秒后添加事件</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">eventloop.loop();</span><br><span class="line"><span class="built_in">setTimeout</span>(<span class="function">() =&gt;</span> &#123;</span><br><span class="line">    eventloop.add(<span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">        <span class="built_in">console</span>.log(<span class="number">1</span>);</span><br><span class="line">    &#125;);</span><br><span class="line">&#125;, <span class="number">800</span>);</span><br><span class="line"><span class="built_in">setTimeout</span>(<span class="function">() =&gt;</span> &#123;</span><br><span class="line">    eventloop.add(<span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">        <span class="built_in">console</span>.log(<span class="number">2</span>);</span><br><span class="line">    &#125;);</span><br><span class="line">&#125;, <span class="number">800</span>);</span><br></pre></td></tr></table></figure>

<p>打印结果为<br>1<br>2</p>
<h4 id="说明添加到事件后会再次执行eventloop-事件循环"><a href="#说明添加到事件后会再次执行eventloop-事件循环" class="headerlink" title="说明添加到事件后会再次执行eventloop 事件循环"></a>说明添加到事件后会再次执行eventloop 事件循环</h4><h3 id="Promise"><a href="#Promise" class="headerlink" title="Promise"></a>Promise</h3><p><img src="https://gitee.com/mo_zi_hao/picsBed/raw/master/image/07promise0.png"></p>
<p>//promise.js<br>promise的resolve和reject会返回一个promise回调,resolve返回执行then,reject返回执行catch,interview这个面试的方法返回一个promise,传入的参数第几轮面试,当三轮面试都通过则打印ok,当其中一轮失败则执行catch打印出在第几轮失败</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line">(<span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">    <span class="keyword">var</span> promise = interview(<span class="number">1</span>)</span><br><span class="line">        .then(<span class="function">() =&gt;</span> &#123;</span><br><span class="line">            <span class="keyword">return</span> interview(<span class="number">2</span>);</span><br><span class="line">        &#125;)</span><br><span class="line">        .then(<span class="function">() =&gt;</span> &#123;</span><br><span class="line">            <span class="keyword">return</span> interview(<span class="number">3</span>);</span><br><span class="line">        &#125;)</span><br><span class="line">        .then(<span class="function">() =&gt;</span> &#123;</span><br><span class="line">            <span class="built_in">console</span>.log(<span class="string">&quot;ok&quot;</span>);</span><br><span class="line">        &#125;)</span><br><span class="line">        .catch(<span class="function"><span class="params">err</span> =&gt;</span> &#123;</span><br><span class="line">            <span class="built_in">console</span>.log(<span class="string">`fail:<span class="subst">$&#123;err.round&#125;</span>round`</span>);</span><br><span class="line">        &#125;);</span><br><span class="line">    <span class="function"><span class="keyword">function</span> <span class="title">interview</span>(<span class="params">round</span>) </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">new</span> <span class="built_in">Promise</span>(<span class="function">(<span class="params">resolve, reject</span>) =&gt;</span> &#123;</span><br><span class="line">            <span class="built_in">setTimeout</span>(<span class="function">() =&gt;</span> &#123;</span><br><span class="line">                <span class="keyword">if</span> (<span class="built_in">Math</span>.random() &gt; <span class="number">0.8</span>) &#123;</span><br><span class="line">                    resolve(<span class="string">&quot;success&quot;</span>);</span><br><span class="line">                &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">                    <span class="keyword">var</span> error = <span class="keyword">new</span> <span class="built_in">Error</span>(<span class="string">&quot;fail&quot;</span>);</span><br><span class="line">                    error.round = round;</span><br><span class="line">                    reject(error);</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;, <span class="number">500</span>);</span><br><span class="line">        &#125;);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;)();</span><br></pre></td></tr></table></figure>

<p>终端执行 node promise.js 打印结果为(打印结果比较随机):<br>fail:2round</p>
<p>//promiseAll.js<br>promise.all是里面的所有promise都执行成功则resolve也就是成功通过,则执行then；否则如果其中一个失败则执行reject,则执行catch（省去了一连串的then，使代码更清晰可读）</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line">(<span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">    <span class="comment">// 两个执行成功则resolve</span></span><br><span class="line">    <span class="built_in">Promise</span>.all([interview(<span class="string">&quot;alibaba&quot;</span>), interview(<span class="string">&quot;tencent&quot;</span>)])</span><br><span class="line">        .then(<span class="function">() =&gt;</span> &#123;</span><br><span class="line">            <span class="built_in">console</span>.log(<span class="string">&quot;success&quot;</span>);</span><br><span class="line">        &#125;)</span><br><span class="line">        .catch(<span class="function"><span class="params">err</span> =&gt;</span> &#123;</span><br><span class="line">            <span class="built_in">console</span>.log(<span class="string">`fail:<span class="subst">$&#123;err.name&#125;</span>`</span>);</span><br><span class="line">        &#125;);</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">function</span> <span class="title">interview</span>(<span class="params">name</span>) </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">new</span> <span class="built_in">Promise</span>(<span class="function">(<span class="params">resolve, reject</span>) =&gt;</span> &#123;</span><br><span class="line">            <span class="built_in">setTimeout</span>(<span class="function">() =&gt;</span> &#123;</span><br><span class="line">                <span class="keyword">if</span> (<span class="built_in">Math</span>.random() &gt; <span class="number">0.2</span>) &#123;</span><br><span class="line">                    resolve(<span class="string">&quot;success&quot;</span>);</span><br><span class="line">                &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">                    <span class="keyword">var</span> error = <span class="keyword">new</span> <span class="built_in">Error</span>(<span class="string">&quot;fail&quot;</span>);</span><br><span class="line">                    error.name = name;</span><br><span class="line">                    reject(error);</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;, <span class="number">500</span>);</span><br><span class="line">        &#125;);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;)();</span><br></pre></td></tr></table></figure>

<p>终端执行 node promiseAll.js 打印结果为(打印结果比较随机):<br>fail:tencent</p>
<h3 id="async、await"><a href="#async、await" class="headerlink" title="async、await"></a>async、await</h3><p><img src="https://gitee.com/mo_zi_hao/picsBed/raw/master/image/async0.png"></p>
<p>async function是Promise的语法糖封装</p>
<h4 id="异步编程的终极方案—以同步的方式写异步"><a href="#异步编程的终极方案—以同步的方式写异步" class="headerlink" title="异步编程的终极方案—以同步的方式写异步"></a>异步编程的终极方案—以同步的方式写异步</h4><p>await关键字可以“暂停”async function的执行<br>await关键字可以以同步的写法获取Promise的执行结果<br>try-catch可以获取await所得到的错误</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//async_await.js</span></span><br><span class="line">(<span class="keyword">async</span> <span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">    <span class="keyword">try</span> &#123;</span><br><span class="line">        <span class="keyword">await</span> interview(<span class="number">1</span>);</span><br><span class="line">        <span class="keyword">await</span> interview(<span class="number">2</span>);</span><br><span class="line">        <span class="keyword">await</span> interview(<span class="number">3</span>);</span><br><span class="line">        <span class="comment">// 并行的异步任务</span></span><br><span class="line">        <span class="comment">// await Promise.all([interview(1), interview(2)]);</span></span><br><span class="line">    &#125; <span class="keyword">catch</span> (e) &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="built_in">console</span>.log(<span class="string">`fail:<span class="subst">$&#123;e.round&#125;</span>`</span>);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="built_in">console</span>.log(<span class="string">&quot;success&quot;</span>);</span><br><span class="line">&#125;)();</span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">interview</span>(<span class="params">round</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="keyword">new</span> <span class="built_in">Promise</span>(<span class="function">(<span class="params">resolve, reject</span>) =&gt;</span> &#123;</span><br><span class="line">        <span class="built_in">setTimeout</span>(<span class="function">() =&gt;</span> &#123;</span><br><span class="line">            <span class="keyword">if</span> (<span class="built_in">Math</span>.random() &gt; <span class="number">0.5</span>) &#123;</span><br><span class="line">                resolve(<span class="string">&quot;success&quot;</span>);</span><br><span class="line">            &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">                <span class="keyword">var</span> error = <span class="keyword">new</span> <span class="built_in">Error</span>(<span class="string">&quot;fail&quot;</span>);</span><br><span class="line">                error.round = round;</span><br><span class="line">                reject(error);</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;, <span class="number">500</span>);</span><br><span class="line">    &#125;);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>





<p>打印结果如下：</p>
<p><img src="https://gitee.com/mo_zi_hao/picsBed/raw/master/image/asyncawait0.jpg"></p>
<h3 id="简单的http服务"><a href="#简单的http服务" class="headerlink" title="简单的http服务"></a>简单的http服务</h3><p><img src="https://gitee.com/mo_zi_hao/picsBed/raw/master/image/%E7%AE%80%E5%8D%95%E7%9A%84http%E6%9C%8D%E5%8A%A10.png"></p>
<p>//index.html</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line">&lt;!DOCTYPE html&gt;</span><br><span class="line">&lt;html lang&#x3D;&quot;en&quot;&gt;</span><br><span class="line">&lt;head&gt;</span><br><span class="line">    &lt;meta charset&#x3D;&quot;UTF-8&quot;&gt;</span><br><span class="line">    &lt;meta name&#x3D;&quot;viewport&quot; content&#x3D;&quot;width&#x3D;device-width, initial-scale&#x3D;1.0&quot;&gt;</span><br><span class="line">    &lt;title&gt;Document&lt;&#x2F;title&gt;</span><br><span class="line">    &lt;style&gt;</span><br><span class="line">        div&#123;</span><br><span class="line">            background-color: skyblue;</span><br><span class="line">            color: white;</span><br><span class="line">        &#125;</span><br><span class="line">    &lt;&#x2F;style&gt;</span><br><span class="line">&lt;&#x2F;head&gt;</span><br><span class="line">&lt;body&gt;</span><br><span class="line">    &lt;div&gt;hello world!&lt;&#x2F;div&gt; </span><br><span class="line">&lt;&#x2F;body&gt;</span><br><span class="line">&lt;&#x2F;html&gt;</span><br></pre></td></tr></table></figure>

<p>//index.js</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line">const http &#x3D; require(&quot;http&quot;); &#x2F;&#x2F;获取http流</span><br><span class="line">const fs &#x3D; require(&quot;fs&quot;);   &#x2F;&#x2F;获取读取文件流</span><br><span class="line"></span><br><span class="line">&#x2F;*</span><br><span class="line">&#x2F;&#x2F; 最简单的http服务--监听3000端口</span><br><span class="line">http.createServer(function(require,response)&#123;</span><br><span class="line">    &#x2F;&#x2F;状态码为200</span><br><span class="line">    response.writeHead(200);</span><br><span class="line">    &#x2F;&#x2F; 返回hello</span><br><span class="line">    response.end(&#39;hello&#39;);</span><br><span class="line">    return;</span><br><span class="line">&#125;).listen(3000)</span><br><span class="line">*&#x2F;</span><br><span class="line"></span><br><span class="line">&#x2F;&#x2F; 返回html文件</span><br><span class="line">&#x2F;&#x2F; 创建服务</span><br><span class="line">http.createServer(function(request, response) &#123;</span><br><span class="line">    &#x2F;&#x2F;路由，favicon.ico  浏览器会默认加载那个ico图标，可在浏览器调试的Network里面看到favicon.ico的请求(见下图)</span><br><span class="line">    if (request.url &#x3D;&#x3D; &quot;&#x2F;favicon.ico&quot;) &#123;</span><br><span class="line">        &#x2F;&#x2F;返回状态码为200</span><br><span class="line">        response.writeHead(200);</span><br><span class="line">        &#x2F;&#x2F;返回为空</span><br><span class="line">        response.end();</span><br><span class="line">        return;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    response.writeHead(200);</span><br><span class="line">    &#x2F;&#x2F;读取index.html文件，并返回</span><br><span class="line">    fs.createReadStream(__dirname + &quot;&#x2F;index.html&quot;).pipe(response);</span><br><span class="line">&#125;).listen(3000)</span><br></pre></td></tr></table></figure>

<p>终端执行 node index.js<br>然后在浏览器打开 localhost:3000</p>
<p><img src="https://gitee.com/mo_zi_hao/picsBed/raw/master/image/%E7%AE%80%E5%8D%95%E7%9A%84http%E6%9C%8D%E5%8A%A1console.jpg"></p>
<p>Network中的favicon.ico的请求</p>
<p><img src="https://gitee.com/mo_zi_hao/picsBed/raw/master/image/network0.jpg"></p>
<h3 id="网页版石头剪刀布游戏"><a href="#网页版石头剪刀布游戏" class="headerlink" title="网页版石头剪刀布游戏"></a>网页版石头剪刀布游戏</h3><p><img src="https://gitee.com/mo_zi_hao/picsBed/raw/master/image/%E5%89%AA%E5%88%80%E7%9F%B3%E5%A4%B4%E5%B8%83.png" alt="剪刀石头布"></p>
<p><img src="https://gitee.com/mo_zi_hao/picsBed/raw/master/image/%E5%89%AA%E5%88%80%E7%9F%B3%E5%A4%B4%E5%B8%83%E6%B8%B8%E6%88%8Fconsole.jpg" alt="剪刀石头布游戏console"></p>
<p>//index.html</p>
<p>首先在html页面中给三个按钮添加点击事件，使用fetch方法发送请求并将对应点击的石头剪刀步作为参数发送给创建好的服务端口，然后获取成功后将返回的文字插入html中</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br></pre></td><td class="code"><pre><span class="line">&lt;!DOCTYPE html&gt;</span><br><span class="line">&lt;html lang&#x3D;&quot;en&quot;&gt;</span><br><span class="line"></span><br><span class="line">&lt;head&gt;</span><br><span class="line">    &lt;meta charset&#x3D;&quot;UTF-8&quot;&gt;</span><br><span class="line">    &lt;meta name&#x3D;&quot;viewport&quot; content&#x3D;&quot;width&#x3D;device-width, initial-scale&#x3D;1.0&quot;&gt;</span><br><span class="line">    &lt;title&gt;石头剪刀布游戏&lt;&#x2F;title&gt;</span><br><span class="line">    &lt;style&gt;</span><br><span class="line">        .wrap &#123;</span><br><span class="line">            width: 300px;</span><br><span class="line">            height: 300px;</span><br><span class="line">            border: 1px solid #000;</span><br><span class="line">            margin-bottom: 10px;</span><br><span class="line">            background-color: #ccc;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        .btn &#123;</span><br><span class="line">            height: 40px;</span><br><span class="line">            width: 80px;</span><br><span class="line">        &#125;</span><br><span class="line">    &lt;&#x2F;style&gt;</span><br><span class="line">&lt;&#x2F;head&gt;</span><br><span class="line"></span><br><span class="line">&lt;body&gt;</span><br><span class="line">    &lt;div id&#x3D;&quot;output&quot; class&#x3D;&quot;wrap&quot;&gt;&lt;&#x2F;div&gt;</span><br><span class="line">    &lt;button id&#x3D;&quot;rock&quot; class&#x3D;&quot;btn&quot;&gt;石头&lt;&#x2F;button&gt;</span><br><span class="line">    &lt;button id&#x3D;&quot;scissor&quot; class&#x3D;&quot;btn&quot;&gt;剪刀&lt;&#x2F;button&gt;</span><br><span class="line">    &lt;button id&#x3D;&quot;paper&quot; class&#x3D;&quot;btn&quot;&gt;布&lt;&#x2F;button&gt;</span><br><span class="line"></span><br><span class="line">&lt;&#x2F;body&gt;</span><br><span class="line">&lt;script&gt;</span><br><span class="line">    function my$(id) &#123;</span><br><span class="line">        return document.getElementById(id)</span><br><span class="line">    &#125;</span><br><span class="line">    const $button &#x3D; &#123;</span><br><span class="line">        rock: my$(&#39;rock&#39;),</span><br><span class="line">        scissor: my$(&#39;scissor&#39;),</span><br><span class="line">        paper: my$(&#39;paper&#39;)</span><br><span class="line">    &#125;</span><br><span class="line">    const $output &#x3D; my$(&#39;output&#39;)</span><br><span class="line">    &#x2F;&#x2F; 遍历对象的方法 Object.keys(obj对象)  返回一个数组</span><br><span class="line">    &#x2F;&#x2F; 给每个按钮添加点击事件,并发送参数action</span><br><span class="line">    Object.keys($button).forEach(key &#x3D;&gt; &#123;</span><br><span class="line">        $button[key].addEventListener(&#39;click&#39;, () &#x3D;&gt; &#123;</span><br><span class="line">            &#x2F;&#x2F;获取目前打开的窗口的端口location.host发送数据action</span><br><span class="line">            fetch(&#96;http:&#x2F;&#x2F;$&#123;location.host&#125;&#x2F;game?action&#x3D;$&#123;key&#125;&#96;)</span><br><span class="line">            .then(res &#x3D;&gt; &#123;</span><br><span class="line">                &#x2F;&#x2F;切记res.text()和res.json()方法不可调用多次否则会报错 body stream is locked</span><br><span class="line">                &#x2F;&#x2F;返回res中的text方法</span><br><span class="line">                return res.text()</span><br><span class="line">            &#125;)</span><br><span class="line">            .then(text &#x3D;&gt; &#123;</span><br><span class="line">                console.log(text)</span><br><span class="line">                &#x2F;&#x2F;获取res.text()方法返回的的文字也就是在index.js中end方法返回的文字</span><br><span class="line">                $output.innerHTML +&#x3D; text + &#39;&lt;br&#x2F;&gt;&#39;</span><br><span class="line">            &#125;)</span><br><span class="line">        &#125;)</span><br><span class="line">    &#125;)</span><br><span class="line">&lt;&#x2F;script&gt;</span><br><span class="line">&lt;&#x2F;html&gt;</span><br></pre></td></tr></table></figure>

<p>//index.js</p>
<p>通过url.parse(request.url)，解析url通过pathname配置各对应的路由（’/‘、’/game’、’/favicon.ico’）的逻辑:<br>‘/‘: 数据通过流的方式，从html(index.html) 文件输出到 http 的请求响应<br>‘/game’: 通过querystring.parse解析通过url.parse(request.url)获取到的url，然后获取客户端传过来的参数action<br>‘/favicon.ico’:加载ico图片，状态码200并返回</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br></pre></td><td class="code"><pre><span class="line">const querystring &#x3D; require(&quot;querystring&quot;);&#x2F;&#x2F;用于解析和格式化URL查询字符串</span><br><span class="line">const http &#x3D; require(&quot;http&quot;);&#x2F;&#x2F;HTTP服务器和客户端，必须使用require(&#39;http&#39;)</span><br><span class="line">const url &#x3D; require(&quot;url&quot;);&#x2F;&#x2F;用于URL解析</span><br><span class="line">const fs &#x3D; require(&quot;fs&quot;);&#x2F;&#x2F;fs模块提供了一种API，用于以与标准POSIX函数紧密相似的方式与文件系统进行交互</span><br><span class="line">const game &#x3D; require(&quot;.&#x2F;game.js&quot;);</span><br><span class="line">&#x2F;&#x2F;判断赢的次数</span><br><span class="line">let playerWon &#x3D; 0;</span><br><span class="line">&#x2F;&#x2F;判断连续出同一个的次数</span><br><span class="line">let sameCount &#x3D; 0;</span><br><span class="line">&#x2F;&#x2F;记录上一次出了什么</span><br><span class="line">let playerLastAction &#x3D; null;</span><br><span class="line">http.createServer(function(request, response) &#123;</span><br><span class="line">    &#x2F;&#x2F;获取url、解析</span><br><span class="line">    const parsedUrl &#x3D; url.parse(request.url);</span><br><span class="line">    &#x2F;&#x2F;匹配路由 &#x2F;favicon.ico</span><br><span class="line">    if (parsedUrl.pathname &#x3D;&#x3D; &quot;&#x2F;favicon.ico&quot;) &#123;</span><br><span class="line">        response.writeHead(200);</span><br><span class="line">        response.end();</span><br><span class="line">        return;</span><br><span class="line">    &#125;</span><br><span class="line">    if (parsedUrl.pathname &#x3D;&#x3D; &quot;&#x2F;game&quot;) &#123;</span><br><span class="line">        &#x2F;&#x2F; 解析url</span><br><span class="line">        const query &#x3D; querystring.parse(parsedUrl.query);</span><br><span class="line">        &#x2F;&#x2F;获取url传进来的参数action</span><br><span class="line">        const playerAction &#x3D; query.action;</span><br><span class="line"></span><br><span class="line">        if (playerWon &gt;&#x3D; 3 || sameCount &#x3D;&#x3D; 9) &#123;</span><br><span class="line">            &#x2F;&#x2F;500 服务端错误</span><br><span class="line">            response.writeHead(500);</span><br><span class="line">            response.end(&quot;我再也不和你玩了&quot;);</span><br><span class="line">            return</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        if (playerLastAction &amp;&amp; playerAction &#x3D;&#x3D; playerLastAction) &#123;</span><br><span class="line">            sameCount++;</span><br><span class="line">        &#125; else &#123;</span><br><span class="line">            sameCount &#x3D; 0;</span><br><span class="line">        &#125;</span><br><span class="line">        playerLastAction &#x3D; playerAction</span><br><span class="line">        if (sameCount &gt;&#x3D; 3) &#123;</span><br><span class="line">            &#x2F;&#x2F;400 客户端输入错误</span><br><span class="line">            response.writeHead(400);</span><br><span class="line">            response.end(&quot;你耍赖我再也不和你玩了&quot;);</span><br><span class="line">            sameCount &#x3D; 9;</span><br><span class="line">            return</span><br><span class="line">        &#125;</span><br><span class="line">        const gameResult &#x3D; game(playerAction);</span><br><span class="line">        response.writeHead(200);</span><br><span class="line">        if (gameResult &#x3D;&#x3D; 0) &#123;</span><br><span class="line">            response.end(&quot;平局&quot;);</span><br><span class="line">        &#125; else if (gameResult &#x3D;&#x3D; 1) &#123;</span><br><span class="line">            playerWon++;</span><br><span class="line">            console.log(playerWon);</span><br><span class="line">            response.end(&quot;你赢了&quot;);</span><br><span class="line">        &#125; else &#123;</span><br><span class="line">            response.end(&quot;你输了&quot;);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    if (parsedUrl.pathname &#x3D;&#x3D; &quot;&#x2F;&quot;) &#123;</span><br><span class="line">        &#x2F;&#x2F; 数据通过流的方式，从html 文件输出到 http 的请求响应</span><br><span class="line">        fs.createReadStream(__dirname + &quot;&#x2F;index.html&quot;).pipe(response);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;).listen(3000);</span><br></pre></td></tr></table></figure>

<p>//game.js</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line">&#x2F;&#x2F; 使用common.js规范,易于维护--模块化</span><br><span class="line">module.exports &#x3D; function(playerAction) &#123;</span><br><span class="line">    &#x2F;&#x2F; 终端创建文件: touch 文件名</span><br><span class="line">    &#x2F;&#x2F; 获取输入的字符,为数组的最后一个</span><br><span class="line">    &#x2F;&#x2F; exports可以挂任何东西,使用require引用文件获取文件</span><br><span class="line">    &#x2F;&#x2F; var playerAction &#x3D; process.argv[process.argv.length - 1];</span><br><span class="line">    var random &#x3D; Math.random() * 3;</span><br><span class="line">    if (random &lt; 1) &#123;</span><br><span class="line">        var computerAction &#x3D; &quot;rock&quot;;</span><br><span class="line">    &#125; else if (1 &lt; random &amp;&amp; random &gt; 2) &#123;</span><br><span class="line">        var computerAction &#x3D; &quot;scissor&quot;;</span><br><span class="line">    &#125; else &#123;</span><br><span class="line">        var computerAction &#x3D; &quot;paper&quot;;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    if (computerAction &#x3D;&#x3D; playerAction) &#123;</span><br><span class="line">        return 0;</span><br><span class="line">    &#125; else if (</span><br><span class="line">        (computerAction &#x3D;&#x3D;&#x3D; &quot;rock&quot; &amp;&amp; playerAction &#x3D;&#x3D;&#x3D; &quot;paper&quot;) ||</span><br><span class="line">        (computerAction &#x3D;&#x3D;&#x3D; &quot;paper&quot; &amp;&amp; playerAction &#x3D;&#x3D;&#x3D; &quot;scissor&quot;) ||</span><br><span class="line">        (computerAction &#x3D;&#x3D;&#x3D; &quot;scissor&quot; &amp;&amp; playerAction &#x3D;&#x3D;&#x3D; &quot;rock&quot;)</span><br><span class="line">    ) &#123;</span><br><span class="line">        return -1;</span><br><span class="line">    &#125; else &#123;</span><br><span class="line">        return 1;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>

<p>终端执行 node index.js<br>然后在浏览器打开 localhost:3000</p>
<p><img src="../../../../%E5%89%AA%E5%88%80%E7%9F%B3%E5%A4%B4%E5%B8%83%E6%B8%B8%E6%88%8Fconsole.jpg" alt="剪刀石头布游戏console"></p>
<p>下面是点击按钮的效果，由于我连续点击 了三遍同一个所以，执行其中的逻辑—服务器会返回500，’’我再也不和你玩了’’</p>
<p><img src="https://gitee.com/mo_zi_hao/picsBed/raw/master/image/%E7%AE%80%E5%8D%95%E7%9F%B3%E5%A4%B4%E5%89%AA%E5%88%80%E5%B8%83%E6%B8%B8%E6%88%8Fc.jpg" alt="简单石头剪刀布游戏c"></p>
<h3 id="express的功能与简单使用"><a href="#express的功能与简单使用" class="headerlink" title="express的功能与简单使用"></a>express的功能与简单使用</h3><h4 id="express的功能特点"><a href="#express的功能特点" class="headerlink" title="express的功能特点:"></a>express的功能特点:</h4><p>一、路由: 在后台服务器里面,路由是后台接收一个请求包然后服务器根据请求包的内容进行分发,把请求包分发到不同的逻辑单元处理—这个分发就叫路由</p>
<p>二、专注的高性能</p>
<p>三、广泛的测试误概率—单元测试</p>
<p>四、http处理简化操作:<br>重定向–服务器会返回一个302的状态码,同时带上一个location(header):后面跟着是你要去的新地址的url<br>location:http://….<br>这就是http的重定向的规范了<br>而express就简化了这个重定向的操作,不用了解这些细节,只需要知道重定向的地址是什么,调用express函数能自动组装好这个http返回包<br>Accept头 —直接浏览器只接收默认格式的返回包,希望服务器也能返回对应格式的包,服务器识别到这个Accept头之后就会返回对应的包—这个就是内容协商</p>
<p>五、模板引擎: 支持14+种模板引擎</p>
<p>六、提供一系列的脚手架</p>
<p>还有一个功能: 中间件 next() 执行回调函数<br>function (req,res,next){<br>….<br>next()<br>}</p>
<h4 id="像express这种调用异步的洋葱模型存在处理缺陷—–对异步的支持不太好———出现了koa框架解决"><a href="#像express这种调用异步的洋葱模型存在处理缺陷—–对异步的支持不太好———出现了koa框架解决" class="headerlink" title="像express这种调用异步的洋葱模型存在处理缺陷—–对异步的支持不太好———出现了koa框架解决"></a>像express这种调用异步的洋葱模型存在处理缺陷—–对异步的支持不太好———出现了koa框架解决</h4><p>下面使用express重写剪刀石头布的游戏</p>
<p>终端输入：npm init 进行初始化<br>然后安装express依赖：npm i express</p>
<p><img src="https://gitee.com/mo_zi_hao/picsBed/raw/master/image/express.png" alt="express"><img src="" alt="图片"></p>
<p>//index.html</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br></pre></td><td class="code"><pre><span class="line">&lt;!DOCTYPE html&gt;</span><br><span class="line">&lt;html lang&#x3D;&quot;en&quot;&gt;</span><br><span class="line"></span><br><span class="line">&lt;head&gt;</span><br><span class="line">    &lt;meta charset&#x3D;&quot;UTF-8&quot;&gt;</span><br><span class="line">    &lt;meta name&#x3D;&quot;viewport&quot; content&#x3D;&quot;width&#x3D;device-width, initial-scale&#x3D;1.0&quot;&gt;</span><br><span class="line">    &lt;title&gt;石头剪刀布游戏&lt;&#x2F;title&gt;</span><br><span class="line">    &lt;style&gt;</span><br><span class="line">        .wrap &#123;</span><br><span class="line">            width: 300px;</span><br><span class="line">            height: 300px;</span><br><span class="line">            border: 1px solid #000;</span><br><span class="line">            margin-bottom: 10px;</span><br><span class="line">            background-color: #ccc;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        .btn &#123;</span><br><span class="line">            height: 40px;</span><br><span class="line">            width: 80px;</span><br><span class="line">        &#125;</span><br><span class="line">    &lt;&#x2F;style&gt;</span><br><span class="line">&lt;&#x2F;head&gt;</span><br><span class="line"></span><br><span class="line">&lt;body&gt;</span><br><span class="line">    &lt;div id&#x3D;&quot;output&quot; class&#x3D;&quot;wrap&quot;&gt;&lt;&#x2F;div&gt;</span><br><span class="line">    &lt;button id&#x3D;&quot;rock&quot; class&#x3D;&quot;btn&quot;&gt;石头&lt;&#x2F;button&gt;</span><br><span class="line">    &lt;button id&#x3D;&quot;scissor&quot; class&#x3D;&quot;btn&quot;&gt;剪刀&lt;&#x2F;button&gt;</span><br><span class="line">    &lt;button id&#x3D;&quot;paper&quot; class&#x3D;&quot;btn&quot;&gt;布&lt;&#x2F;button&gt;</span><br><span class="line"></span><br><span class="line">&lt;&#x2F;body&gt;</span><br><span class="line">&lt;script&gt;</span><br><span class="line">    function my$(id) &#123;</span><br><span class="line">        return document.getElementById(id)</span><br><span class="line">    &#125;</span><br><span class="line">    const $button &#x3D; &#123;</span><br><span class="line">        rock: my$(&#39;rock&#39;),</span><br><span class="line">        scissor: my$(&#39;scissor&#39;),</span><br><span class="line">        paper: my$(&#39;paper&#39;)</span><br><span class="line">    &#125;</span><br><span class="line">    const $output &#x3D; my$(&#39;output&#39;)</span><br><span class="line">    &#x2F;&#x2F; 遍历对象的方法 Object.keys(obj对象)  返回一个数组</span><br><span class="line">    &#x2F;&#x2F; 给每个按钮添加点击事件,并发送参数action</span><br><span class="line">    Object.keys($button).forEach(key &#x3D;&gt; &#123;</span><br><span class="line">        $button[key].addEventListener(&#39;click&#39;, () &#x3D;&gt; &#123;</span><br><span class="line">            fetch(&#96;http:&#x2F;&#x2F;$&#123;location.host&#125;&#x2F;game?action&#x3D;$&#123;key&#125;&#96;)</span><br><span class="line">            .then(res &#x3D;&gt; &#123;</span><br><span class="line">                &#x2F;&#x2F; console.log(res.text())</span><br><span class="line">                return res.text()</span><br><span class="line">            &#125;)</span><br><span class="line">            .then(text &#x3D;&gt; &#123;</span><br><span class="line">                console.log(text)</span><br><span class="line">                $output.innerHTML +&#x3D; text + &#39;&lt;br&#x2F;&gt;&#39;</span><br><span class="line">            &#125;)</span><br><span class="line">            .catch(err&#x3D;&gt;&#123;</span><br><span class="line">                console.log(err)</span><br><span class="line">            &#125;)</span><br><span class="line">        &#125;)</span><br><span class="line">    &#125;)</span><br><span class="line">&lt;&#x2F;script&gt;</span><br><span class="line">&lt;&#x2F;html&gt;</span><br></pre></td></tr></table></figure>

<p>//game.js</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line">&#x2F;&#x2F; 使用common.js规范,易于维护--模块化</span><br><span class="line">module.exports &#x3D; function(playerAction) &#123;</span><br><span class="line">    &#x2F;&#x2F; 终端创建文件: touch 文件名</span><br><span class="line">    &#x2F;&#x2F; 获取输入的字符,为数组的最后一个</span><br><span class="line">    &#x2F;&#x2F; exports可以挂任何东西,使用require引用文件获取文件</span><br><span class="line">    &#x2F;&#x2F; var playerAction &#x3D; process.argv[process.argv.length - 1];</span><br><span class="line">    var random &#x3D; Math.random() * 3;</span><br><span class="line">    if (random &lt; 1) &#123;</span><br><span class="line">        var computerAction &#x3D; &quot;rock&quot;;</span><br><span class="line">    &#125; else if (1 &lt; random &amp;&amp; random &gt; 2) &#123;</span><br><span class="line">        var computerAction &#x3D; &quot;scissor&quot;;</span><br><span class="line">    &#125; else &#123;</span><br><span class="line">        var computerAction &#x3D; &quot;paper&quot;;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    if (computerAction &#x3D;&#x3D; playerAction) &#123;</span><br><span class="line">        return 0;</span><br><span class="line">    &#125; else if (</span><br><span class="line">        (computerAction &#x3D;&#x3D;&#x3D; &quot;rock&quot; &amp;&amp; playerAction &#x3D;&#x3D;&#x3D; &quot;paper&quot;) ||</span><br><span class="line">        (computerAction &#x3D;&#x3D;&#x3D; &quot;paper&quot; &amp;&amp; playerAction &#x3D;&#x3D;&#x3D; &quot;scissor&quot;) ||</span><br><span class="line">        (computerAction &#x3D;&#x3D;&#x3D; &quot;scissor&quot; &amp;&amp; playerAction &#x3D;&#x3D;&#x3D; &quot;rock&quot;)</span><br><span class="line">    ) &#123;</span><br><span class="line">        return -1;</span><br><span class="line">    &#125; else &#123;</span><br><span class="line">        return 1;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>

<h4 id="express的使用"><a href="#express的使用" class="headerlink" title="express的使用"></a>express的使用</h4><p>//index.js</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br></pre></td><td class="code"><pre><span class="line">const fs &#x3D; require(&quot;fs&quot;);</span><br><span class="line">const game &#x3D; require(&quot;.&#x2F;game.js&quot;);</span><br><span class="line">const express &#x3D; require(&#39;express&#39;)  &#x2F;&#x2F;获取express</span><br><span class="line">&#x2F;&#x2F;判断赢的次数</span><br><span class="line">let playerWon &#x3D; 0;</span><br><span class="line">&#x2F;&#x2F;判断连续出同一个的次数</span><br><span class="line">let sameCount &#x3D; 0;</span><br><span class="line">&#x2F;&#x2F;记录上一次出了什么</span><br><span class="line">let playerLastAction &#x3D; null;</span><br><span class="line"></span><br><span class="line">const app &#x3D; express();   &#x2F;&#x2F;创建服务</span><br><span class="line"></span><br><span class="line">&#x2F;&#x2F;使用路由</span><br><span class="line">app.get(&#39;&#x2F;favicon.ico&#39;,(request,response)&#x3D;&gt;&#123;</span><br><span class="line">    response.status(200)</span><br><span class="line">    response.send()</span><br><span class="line">    return</span><br><span class="line">&#125;)</span><br><span class="line">app.get(&#39;&#x2F;game&#39;,(request,response)&#x3D;&gt;&#123;</span><br><span class="line">    response.status(200)</span><br><span class="line">    &#x2F;&#x2F;解析url</span><br><span class="line">    const query &#x3D; request.query</span><br><span class="line">    &#x2F;&#x2F;获取url传过来的action参数</span><br><span class="line">    const playerAction &#x3D; query.action</span><br><span class="line"></span><br><span class="line">    if (playerWon &gt;&#x3D; 3 || sameCount &#x3D;&#x3D; 9) &#123;</span><br><span class="line">        &#x2F;&#x2F;500 服务端错误</span><br><span class="line">        response.status(500)</span><br><span class="line">        response.send(&#39;我再也不和你玩了&#39;)</span><br><span class="line">        &#x2F;&#x2F; response.writeHead(500);</span><br><span class="line">        &#x2F;&#x2F; response.end(&quot;我再也不和你玩了&quot;);</span><br><span class="line">        return</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    if (playerLastAction &amp;&amp; playerAction &#x3D;&#x3D; playerLastAction) &#123;</span><br><span class="line">        sameCount++;</span><br><span class="line">    &#125; else &#123;</span><br><span class="line">        sameCount &#x3D; 0;</span><br><span class="line">    &#125;</span><br><span class="line">    playerLastAction &#x3D; playerAction</span><br><span class="line">    if (sameCount &gt;&#x3D; 3) &#123;</span><br><span class="line">        &#x2F;&#x2F;400 客户端输入错误</span><br><span class="line">        response.status(400)</span><br><span class="line">        response.send(&#39;你耍赖我再也不和你玩了&#39;)</span><br><span class="line">        &#x2F;&#x2F; response.writeHead(400);</span><br><span class="line">        &#x2F;&#x2F; response.end(&quot;你耍赖我再也不和你玩了&quot;);</span><br><span class="line">        sameCount &#x3D; 9;</span><br><span class="line">        return</span><br><span class="line">    &#125;</span><br><span class="line">    const gameResult &#x3D; game(playerAction);</span><br><span class="line">    &#x2F;&#x2F; response.writeHead(200);</span><br><span class="line">    response.status(200)</span><br><span class="line">    if (gameResult &#x3D;&#x3D; 0) &#123;</span><br><span class="line"></span><br><span class="line">        response.send(  &quot;平局&quot;);</span><br><span class="line">    &#125; else if (gameResult &#x3D;&#x3D; 1) &#123;</span><br><span class="line">        playerWon++;</span><br><span class="line">        console.log(playerWon);</span><br><span class="line">        &#x2F;&#x2F; response.end(&quot;你赢了&quot;);</span><br><span class="line">        response.send(&quot;你赢了&quot;)</span><br><span class="line"></span><br><span class="line">    &#125; else &#123;</span><br><span class="line">        &#x2F;&#x2F; response.end(&quot;你输了&quot;);</span><br><span class="line">        response.send(&quot;你输了&quot;)</span><br><span class="line">    &#125;</span><br><span class="line">&#125;)</span><br><span class="line">app.get(&#39;&#x2F;&#39;,(request,response)&#x3D;&gt;&#123;</span><br><span class="line">    response.status(200)</span><br><span class="line">    response.send(fs.readFileSync(__dirname+&#39;&#x2F;index.html&#39;,&#39;utf-8&#39;))</span><br><span class="line">    &#x2F;&#x2F; fs.createReadStream(__dirname + &quot;&#x2F;index.html&quot;).pipe(response);</span><br><span class="line"></span><br><span class="line">    console.log(&quot;进入页面&quot;)</span><br><span class="line">    return</span><br><span class="line">&#125;)</span><br><span class="line"></span><br><span class="line">&#x2F;&#x2F;监听3000端口</span><br><span class="line">app.listen(3000,()&#x3D;&gt;&#123;</span><br><span class="line">    console.log(&#39;监听3000&#39;)</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>

<p>终端执行 node index.js<br>然后在浏览器打开 localhost:3000</p>
<h3 id="Koa的功能与简单使用"><a href="#Koa的功能与简单使用" class="headerlink" title="Koa的功能与简单使用"></a>Koa的功能与简单使用</h3><h4 id="Koa的核心功能"><a href="#Koa的核心功能" class="headerlink" title="Koa的核心功能"></a>Koa的核心功能</h4><p>使用async function实现的中间件：</p>
<ul>
<li>有“暂停执行”的能力</li>
<li>在异步的情况下也符合洋葱模型</li>
<li>精简内核，所有额外功能都转移到中间件里实现</li>
</ul>
<h4 id="Express的核心功能"><a href="#Express的核心功能" class="headerlink" title="Express的核心功能"></a>Express的核心功能</h4><ul>
<li>路由</li>
<li>request/response简化<ul>
<li>request：pathname、query等</li>
<li>response：send()、json()、jsonp()等</li>
</ul>
</li>
</ul>
<p><em>Koa VS Express</em></p>
<ul>
<li>express门槛更低，koa更强大优雅</li>
<li>express封装更多东西，开发更快，koa可定制性更高</li>
<li>koa比express更极致的request/response简化:<ul>
<li>koa：<ul>
<li>ctx.status = 200</li>
<li>ctx.body = ‘hello world’</li>
</ul>
</li>
<li>express：<ul>
<li>response.status(200)</li>
<li>response.send(‘hello world’)</li>
</ul>
</li>
</ul>
</li>
</ul>
<p><em>Koa的特点：</em></p>
<ul>
<li>中间件</li>
<li>请求和返回的处理</li>
<li>koa不会绑定任何的中间件，路由放在中间件上，通过中间件实现想要的路由</li>
<li>koa不会带任何的路由中间件</li>
</ul>
<p><em>那么下面我们使用koa、koa-mount实现一个简单的石头剪刀布游戏</em></p>
<ul>
<li>首先终端打开，初始化我们的项目，然后安装npm依赖</li>
</ul>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">npm init</span><br><span class="line">npm i koa koa-mount</span><br></pre></td></tr></table></figure>


<p><img src="https://gitee.com/mo_zi_hao/picsBed/raw/master/image/express.png"></p>
<p><em>index.js</em></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br></pre></td><td class="code"><pre><span class="line">const koa &#x3D; require(&quot;koa&quot;);</span><br><span class="line">const mount &#x3D; require(&quot;koa-mount&quot;);</span><br><span class="line">const game &#x3D; require(&quot;.&#x2F;game.js&quot;);</span><br><span class="line">const fs &#x3D; require(&quot;fs&quot;);</span><br><span class="line"></span><br><span class="line">const app &#x3D; new koa();</span><br><span class="line"></span><br><span class="line">&#x2F;&#x2F;判断赢的次数</span><br><span class="line">let playerWon &#x3D; 0;</span><br><span class="line">&#x2F;&#x2F;判断连续出同一个的次数</span><br><span class="line">let sameCount &#x3D; 0;</span><br><span class="line">&#x2F;&#x2F;记录上一次出了什么</span><br><span class="line">let playerLastAction &#x3D; null;</span><br><span class="line"></span><br><span class="line">app.use(</span><br><span class="line">    mount(&quot;&#x2F;favicon.ico&quot;, function(ctx) &#123;</span><br><span class="line">        ctx.status &#x3D; 200;</span><br><span class="line">        return;</span><br><span class="line">    &#125;)</span><br><span class="line">);</span><br><span class="line"></span><br><span class="line">const gameKoa &#x3D; new koa();</span><br><span class="line">app.use(mount(&quot;&#x2F;game&quot;, gameKoa));</span><br><span class="line"></span><br><span class="line">&#x2F;&#x2F; 将所有的中间件使用gameKoa.use挂载</span><br><span class="line">gameKoa.use(async function(ctx, next) &#123;</span><br><span class="line">    console.log(ctx.query.action);</span><br><span class="line">    const playerAction &#x3D; ctx.query.action;</span><br><span class="line">    const result &#x3D; game(playerAction);</span><br><span class="line"></span><br><span class="line">    if (playerWon &gt;&#x3D; 3 || sameCount &#x3D;&#x3D; 9) &#123;</span><br><span class="line">        &#x2F;&#x2F;500 服务端错误</span><br><span class="line">        ctx.status &#x3D; 500;</span><br><span class="line">        ctx.body &#x3D; &quot;我再也不和你玩了&quot;;</span><br><span class="line">        return;</span><br><span class="line">    &#125;</span><br><span class="line">    if (playerLastAction &amp;&amp; playerAction &#x3D;&#x3D; playerLastAction) &#123;</span><br><span class="line">        sameCount++;</span><br><span class="line">    &#125; else &#123;</span><br><span class="line">        sameCount &#x3D; 0;</span><br><span class="line">    &#125;</span><br><span class="line">    playerLastAction &#x3D; playerAction;</span><br><span class="line">    if (sameCount &gt;&#x3D; 3) &#123;</span><br><span class="line">        &#x2F;&#x2F;400 客户端输入错误</span><br><span class="line">        ctx.status &#x3D; 400;</span><br><span class="line">        ctx.body &#x3D; &quot;你耍赖我再也不和你玩了&quot;;</span><br><span class="line">        sameCount &#x3D; 9;</span><br><span class="line">        return;</span><br><span class="line">    &#125;</span><br><span class="line">    await new Promise(resolve &#x3D;&gt; &#123;</span><br><span class="line">        setTimeout(() &#x3D;&gt; &#123;</span><br><span class="line">            ctx.status &#x3D; 200;</span><br><span class="line">            if (result &#x3D;&#x3D; 0) &#123;</span><br><span class="line">                ctx.body &#x3D; &quot;平局&quot;;</span><br><span class="line">            &#125; else if (result &#x3D;&#x3D; -1) &#123;</span><br><span class="line">                ctx.body &#x3D; &quot;你输了&quot;;</span><br><span class="line">            &#125; else &#123;</span><br><span class="line">                ctx.body &#x3D; &quot;你赢了&quot;;</span><br><span class="line">                ctx.playerWon &#x3D; true;</span><br><span class="line">            &#125;</span><br><span class="line">            resolve();</span><br><span class="line">        &#125;, 500);</span><br><span class="line">    &#125;);</span><br><span class="line">    if (ctx.playerWon) &#123;</span><br><span class="line">        playerWon++;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;);</span><br><span class="line">app.use(</span><br><span class="line">    mount(&quot;&#x2F;&quot;, function(ctx) &#123;</span><br><span class="line">        ctx.body &#x3D; fs.readFileSync(__dirname + &quot;&#x2F;index.html&quot;, &quot;utf-8&quot;);</span><br><span class="line">    &#125;)</span><br><span class="line">);</span><br><span class="line"></span><br><span class="line">app.listen(3000);</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p><em>index.html</em></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br></pre></td><td class="code"><pre><span class="line">&lt;!DOCTYPE html&gt;</span><br><span class="line">&lt;html lang&#x3D;&quot;en&quot;&gt;</span><br><span class="line"></span><br><span class="line">&lt;head&gt;</span><br><span class="line">    &lt;meta charset&#x3D;&quot;UTF-8&quot;&gt;</span><br><span class="line">    &lt;meta name&#x3D;&quot;viewport&quot; content&#x3D;&quot;width&#x3D;device-width, initial-scale&#x3D;1.0&quot;&gt;</span><br><span class="line">    &lt;title&gt;石头剪刀布游戏&lt;&#x2F;title&gt;</span><br><span class="line">    &lt;style&gt;</span><br><span class="line">        .wrap &#123;</span><br><span class="line">            width: 300px;</span><br><span class="line">            height: 300px;</span><br><span class="line">            border: 1px solid #000;</span><br><span class="line">            margin-bottom: 10px;</span><br><span class="line">            background-color: #ccc;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        .btn &#123;</span><br><span class="line">            height: 40px;</span><br><span class="line">            width: 80px;</span><br><span class="line">        &#125;</span><br><span class="line">    &lt;&#x2F;style&gt;</span><br><span class="line">&lt;&#x2F;head&gt;</span><br><span class="line"></span><br><span class="line">&lt;body&gt;</span><br><span class="line">    &lt;div id&#x3D;&quot;output&quot; class&#x3D;&quot;wrap&quot;&gt;&lt;&#x2F;div&gt;</span><br><span class="line">    &lt;button id&#x3D;&quot;rock&quot; class&#x3D;&quot;btn&quot;&gt;石头&lt;&#x2F;button&gt;</span><br><span class="line">    &lt;button id&#x3D;&quot;scissor&quot; class&#x3D;&quot;btn&quot;&gt;剪刀&lt;&#x2F;button&gt;</span><br><span class="line">    &lt;button id&#x3D;&quot;paper&quot; class&#x3D;&quot;btn&quot;&gt;布&lt;&#x2F;button&gt;</span><br><span class="line">   </span><br><span class="line">&lt;&#x2F;body&gt;</span><br><span class="line">&lt;script&gt;</span><br><span class="line">    function my$(id) &#123;</span><br><span class="line">        return document.getElementById(id)</span><br><span class="line">    &#125;</span><br><span class="line">    const $button &#x3D; &#123;</span><br><span class="line">        rock: my$(&#39;rock&#39;),</span><br><span class="line">        scissor: my$(&#39;scissor&#39;),</span><br><span class="line">        paper: my$(&#39;paper&#39;)</span><br><span class="line">    &#125;</span><br><span class="line">    const $output &#x3D; my$(&#39;output&#39;)</span><br><span class="line">    &#x2F;&#x2F; 遍历对象的方法 Object.keys(obj对象)  返回一个数组</span><br><span class="line">    &#x2F;&#x2F; 给每个按钮添加点击事件,并发送参数action</span><br><span class="line">    Object.keys($button).forEach(key &#x3D;&gt; &#123;</span><br><span class="line">        $button[key].addEventListener(&#39;click&#39;, () &#x3D;&gt; &#123;</span><br><span class="line">            fetch(&#96;http:&#x2F;&#x2F;$&#123;location.host&#125;&#x2F;game?action&#x3D;$&#123;key&#125;&#96;)</span><br><span class="line">            .then(res &#x3D;&gt; &#123;</span><br><span class="line">                &#x2F;&#x2F; console.log(res.text())</span><br><span class="line">                return res.text()</span><br><span class="line">            &#125;)</span><br><span class="line">            .then(text &#x3D;&gt; &#123;</span><br><span class="line">                &#x2F;&#x2F; console.log(text)  &#x2F;&#x2F;console.log要注释掉否则会报错</span><br><span class="line">                $output.innerHTML +&#x3D; text + &#39;&lt;br&#x2F;&gt;&#39;</span><br><span class="line">            &#125;)</span><br><span class="line">        &#125;)</span><br><span class="line">    &#125;)</span><br><span class="line">&lt;&#x2F;script&gt;</span><br><span class="line">&lt;&#x2F;html&gt;</span><br></pre></td></tr></table></figure>

<p><em>game.js</em> </p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line">&#x2F;&#x2F; 使用common.js规范,易于维护--模块化</span><br><span class="line">module.exports &#x3D; function(playerAction) &#123;</span><br><span class="line">    &#x2F;&#x2F; 终端创建文件: touch 文件名</span><br><span class="line">    &#x2F;&#x2F; 获取输入的字符,为数组的最后一个</span><br><span class="line">    &#x2F;&#x2F; exports可以挂任何东西,使用require引用文件获取文件</span><br><span class="line">    &#x2F;&#x2F; var playerAction &#x3D; process.argv[process.argv.length - 1];</span><br><span class="line">    var random &#x3D; Math.random() * 3;</span><br><span class="line">    if (random &lt; 1) &#123;</span><br><span class="line">        var computerAction &#x3D; &quot;rock&quot;;</span><br><span class="line">    &#125; else if (1 &lt; random &amp;&amp; random &gt; 2) &#123;</span><br><span class="line">        var computerAction &#x3D; &quot;scissor&quot;;</span><br><span class="line">    &#125; else &#123;</span><br><span class="line">        var computerAction &#x3D; &quot;paper&quot;;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    if (computerAction &#x3D;&#x3D; playerAction) &#123;</span><br><span class="line">        return 0;</span><br><span class="line">    &#125; else if (</span><br><span class="line">        (computerAction &#x3D;&#x3D;&#x3D; &quot;rock&quot; &amp;&amp; playerAction &#x3D;&#x3D;&#x3D; &quot;paper&quot;) ||</span><br><span class="line">        (computerAction &#x3D;&#x3D;&#x3D; &quot;paper&quot; &amp;&amp; playerAction &#x3D;&#x3D;&#x3D; &quot;scissor&quot;) ||</span><br><span class="line">        (computerAction &#x3D;&#x3D;&#x3D; &quot;scissor&quot; &amp;&amp; playerAction &#x3D;&#x3D;&#x3D; &quot;rock&quot;)</span><br><span class="line">    ) &#123;</span><br><span class="line">        return -1;</span><br><span class="line">    &#125; else &#123;</span><br><span class="line">        return 1;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>


<p><em>终端打开执行</em></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">node index.js</span><br></pre></td></tr></table></figure>

<ul>
<li>然后浏览器打开localhost:3000</li>
</ul>
<p><img src="https://gitee.com/mo_zi_hao/picsBed/raw/master/image/640.webp"></p>
<p><em>以上就是使用koa、koa-mount实现一个简单石头剪刀布游戏过程了。相比express，koa对异步有着更好的支持，更好的处理这种调用异步的洋葱模型</em></p>
</article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta">文章作者: </span><span class="post-copyright-info"><a href="mailto:undefined">mozihao</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta">文章链接: </span><span class="post-copyright-info"><a href="https://mo_zi_hao.gitee.io/blog/posts/25418/">https://mo_zi_hao.gitee.io/blog/posts/25418/</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta">版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来自 <a href="https://mo_zi_hao.gitee.io/blog" target="_blank">mozihao</a>！</span></div></div><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/blog/tags/node-js/">node.js</a></div><div class="post_share"><div class="social-share" data-image="https://gitee.com/mo_zi_hao/picsBed/raw/master/image/%E8%8B%B1%E8%AF%AD%E5%AD%A6%E4%B9%A0%E8%8B%B1%E6%A0%87%E5%A4%A7%E5%85%A8.jpg" data-sites="facebook,twitter,wechat,weibo,qq"></div><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/social-share.js/dist/css/share.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/npm/social-share.js/dist/js/social-share.min.js" defer></script></div></div><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="/blog/posts/28371/"><img class="prev-cover" src="https://gitee.com/mo_zi_hao/picsBed/raw/master/image/%E8%8B%B1%E8%AF%AD%E5%AD%A6%E4%B9%A0%E8%8B%B1%E6%A0%87%E5%A4%A7%E5%85%A8.jpg" onerror="onerror=null;src='/blog/img/404.jpg'" alt="cover of previous post"><div class="pagination-info"><div class="label">上一篇</div><div class="prev_info">node.js --- Buffer</div></div></a></div><div class="next-post pull-right"><a href="/blog/posts/15317/"><img class="next-cover" src="https://gitee.com/mo_zi_hao/picsBed/raw/master/image/%E6%94%B6%E7%9B%8A%E6%8E%92%E8%A1%8C%E6%A6%9C.jpg" onerror="onerror=null;src='/blog/img/404.jpg'" alt="cover of next post"><div class="pagination-info"><div class="label">下一篇</div><div class="next_info">CSS-3D动画旋转展示</div></div></a></div></nav></div><div class="aside-content" id="aside-content"><div class="card-widget card-info"><div class="card-info-avatar is-center"><img class="avatar-img" src="https://gitee.com/mo_zi_hao/picsBed/raw/master/image/%E6%B5%B7%E8%B4%BC008.jpg" onerror="this.onerror=null;this.src='/blog/img/friend_404.gif'" alt="avatar"/><div class="author-info__name">mozihao</div><div class="author-info__description">if you risk nothing,then you risk anthing</div></div><div class="card-info-data"><div class="card-info-data-item is-center"><a href="/blog/archives/"><div class="headline">文章</div><div class="length-num">24</div></a></div><div class="card-info-data-item is-center"><a href="/blog/tags/"><div class="headline">标签</div><div class="length-num">16</div></a></div><div class="card-info-data-item is-center"><a href="/blog/categories/"><div class="headline">分类</div><div class="length-num">11</div></a></div></div><a class="button--animated" id="card-info-btn" target="_blank" rel="noopener" href="https://gitee.com/mo_zi_hao"><i class="iconfont icon-gitee-fill-round"></i><span>我的Gitee</span></a><div class="card-info-social-icons is-center"><a class="social-icon" href="https://gitee.com/mo_zi_hao" target="_blank" title="Gitee"><i class="fab iconfont icon-gitee-fill-round"></i></a><a class="social-icon" href="https://github.com/mmCrazy" target="_blank" title="Github"><i class="fab fa-github"></i></a><a class="social-icon" href="https://blog.csdn.net/xiaomohhh?spm=1001.2101.3001.5343" target="_blank" title="CSDN"><i class="fab iconfont icon-csdn"></i></a><a class="social-icon" href="https://gitee.com/mo_zi_hao/picsBed/raw/master/image/qrcode_for_gh_d35478eda980_430.jpg" target="_blank" title="公众号"><i class="fab iconfont icon-weixingongzhonghao"></i></a><a class="social-icon" href="mailto:747304031@qq.com" target="_blank" title="Email"><i class="fas fa-envelope"></i></a></div></div><div class="card-widget card-announcement"><div class="item-headline"><i class="fas fa-bullhorn card-announcement-animation"></i><span>公告</span></div><div class="announcement_content">如文章中有哪些错漏，欢迎各位前端的大佬与我交流并指正，谢谢!!</br>邮箱：747304031@qq.com</br>公众号：一起写前端bug</div></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>目录</span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-3"><a class="toc-link" href="#CommomJS%E8%A7%84%E8%8C%83-%E5%A4%96%E9%83%A8%E7%9A%84%E5%BC%95%E7%94%A8exports%E5%92%8Cmodule-exports"><span class="toc-number">1.</span> <span class="toc-text">CommomJS规范: 外部的引用exports和module.exports</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#exports"><span class="toc-number">1.1.</span> <span class="toc-text">exports</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#module-exports"><span class="toc-number">1.2.</span> <span class="toc-text">module.exports</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E6%98%BE%E7%84%B6%E5%A4%96%E9%83%A8%E7%9A%84%E5%BC%95%E7%94%A8%E4%BC%9A%E8%A2%AB%E8%BF%99%E4%B8%AAmodule-exports%E5%8F%96%E4%BB%A3-%E4%BC%9A%E8%A6%86%E7%9B%96%E6%89%80%E6%9C%89%E7%9A%84exports"><span class="toc-number">1.3.</span> <span class="toc-text">显然外部的引用会被这个module.exports取代,会覆盖所有的exports</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E7%AE%80%E5%8D%95%E7%9A%84%E7%9F%B3%E5%A4%B4%E5%89%AA%E5%88%80%E5%B8%83%E6%B8%B8%E6%88%8F"><span class="toc-number">2.</span> <span class="toc-text">简单的石头剪刀布游戏</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E8%8E%B7%E5%8F%96%E8%BE%93%E5%85%A5%E7%9A%84%E6%96%B9%E6%B3%95%E8%BF%98%E6%9C%89%E5%B0%B1%E6%98%AF%E4%BD%BF%E7%94%A8%E8%BF%9B%E7%A8%8B%E6%8E%A7%E5%88%B6%E5%8F%B0%E8%BE%93%E5%85%A5%E6%B5%81-process-stdin-%E4%B8%8B%E9%9D%A2%E4%BD%BF%E7%94%A8%E8%BF%9B%E7%A8%8B%E6%8E%A7%E5%88%B6%E5%8F%B0%E8%BE%93%E5%85%A5%E6%B5%81%E7%9A%84%E6%9D%A5%E8%8E%B7%E5%8F%96%E6%88%91%E4%BB%AC%E5%9C%A8%E7%BB%88%E7%AB%AF%E8%BE%93%E5%85%A5%E7%9A%84%E5%AD%97%E7%AC%A6"><span class="toc-number">2.1.</span> <span class="toc-text">获取输入的方法还有就是使用进程控制台输入流, process.stdin,下面使用进程控制台输入流的来获取我们在终端输入的字符</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E9%80%9A%E8%BF%87%E8%BF%9B%E7%A8%8B%E6%8E%A7%E5%88%B6%E5%8F%B0%E8%BE%93%E5%85%A5%E6%B5%81%E8%8E%B7%E5%8F%96%E5%9C%A8%E7%BB%88%E7%AB%AF%E8%BE%93%E5%85%A5%E7%9A%84%E6%95%B0%E6%8D%AEprocess-stdin%E8%8E%B7%E5%8F%96%E8%BE%93%E5%85%A5%E7%9A%84%E6%95%B0%E6%8D%AE"><span class="toc-number">2.2.</span> <span class="toc-text">通过进程控制台输入流获取在终端输入的数据process.stdin获取输入的数据</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#node-js-API%E8%A7%A3%E8%AF%BB-process"><span class="toc-number">3.</span> <span class="toc-text">node.js(API解读) - process</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#Event-%E2%80%98exit%E2%80%99"><span class="toc-number">3.1.</span> <span class="toc-text">Event: ‘exit’</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#Event-%E2%80%98uncaughtException%E2%80%99"><span class="toc-number">3.2.</span> <span class="toc-text">Event: ‘uncaughtException’</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#Signal-Events"><span class="toc-number">3.3.</span> <span class="toc-text">Signal Events</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#process-stdout"><span class="toc-number">3.4.</span> <span class="toc-text">process.stdout</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#process-stdin"><span class="toc-number">3.5.</span> <span class="toc-text">process.stdin</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#process-argv"><span class="toc-number">3.6.</span> <span class="toc-text">process.argv</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#process-execPath"><span class="toc-number">3.7.</span> <span class="toc-text">process.execPath</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#process-chdir-directory-%E5%92%8C-process-cwd"><span class="toc-number">3.8.</span> <span class="toc-text">process.chdir(directory) 和 process.cwd()</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#process-env"><span class="toc-number">3.9.</span> <span class="toc-text">process.env</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#process-exit-code-0"><span class="toc-number">3.10.</span> <span class="toc-text">process.exit(code&#x3D;0)</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#process-getgid-%E3%80%81process-setgid-id-%E3%80%81process-getuid-%E3%80%81process-setuid-id"><span class="toc-number">3.11.</span> <span class="toc-text">process.getgid()、process.setgid(id)、process.getuid()、process.setuid(id)</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#process-version%E3%80%81process-versions"><span class="toc-number">3.12.</span> <span class="toc-text">process.version、process.versions</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#process-installPrefix"><span class="toc-number">3.13.</span> <span class="toc-text">process.installPrefix</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#process-kill-pid-signal-%E2%80%99SIGTERM%E2%80%99"><span class="toc-number">3.14.</span> <span class="toc-text">process.kill(pid, signal&#x3D;’SIGTERM’)</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#process-pid%E3%80%81process-title%E3%80%81process-arch%E3%80%81process-platform"><span class="toc-number">3.15.</span> <span class="toc-text">process.pid、process.title、process.arch、process.platform</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#process-memoryUsage"><span class="toc-number">3.16.</span> <span class="toc-text">process.memoryUsage()</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#process-nextTick-callback"><span class="toc-number">3.17.</span> <span class="toc-text">process.nextTick(callback)</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#process-umask-mask"><span class="toc-number">3.18.</span> <span class="toc-text">process.umask([mask])</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#process-uptime"><span class="toc-number">3.19.</span> <span class="toc-text">process.uptime()</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#process-reallyExit-status"><span class="toc-number">3.20.</span> <span class="toc-text">process.reallyExit(status)</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#process-kill-pid-sig"><span class="toc-number">3.21.</span> <span class="toc-text">process._kill(pid,sig)</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#process-binding-name"><span class="toc-number">3.22.</span> <span class="toc-text">process.binding(name)</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E8%A7%82%E5%AF%9F%E8%80%85%E6%A8%A1%E5%BC%8F"><span class="toc-number">4.</span> <span class="toc-text">观察者模式</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E4%BA%8B%E4%BB%B6%E8%A7%A6%E5%8F%91%E7%9B%91%E5%90%ACnewlesson%E4%BA%8B%E4%BB%B6%EF%BC%8C%E8%BF%9B%E8%A1%8C%E6%96%B9%E6%B3%95%E5%9B%9E%E8%B0%83"><span class="toc-number">4.1.</span> <span class="toc-text">事件触发监听newlesson事件，进行方法回调</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E9%9D%9E%E9%98%BB%E5%A1%9EI-O"><span class="toc-number">5.</span> <span class="toc-text">非阻塞I&#x2F;O</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#Node-js%E7%9A%84%E6%89%80%E6%9C%89I-O%E6%93%8D%E4%BD%9C%E9%83%BD%E6%98%AF%E9%9D%9E%E9%98%BB%E5%A1%9E%E7%9A%84-%E4%BC%9A%E6%8A%8A%E5%A4%A7%E9%87%8F%E7%9A%84%E8%AE%A1%E7%AE%97%E8%83%BD%E5%8A%9B%E5%88%86%E5%8F%91%E7%BB%99%E5%85%B6%E4%BB%96c-%E7%BA%BF%E7%A8%8B%E8%AE%A1%E7%AE%97%E7%AD%89%E5%88%B0%E5%85%B6%E4%BB%96c-%E7%BA%BF%E7%A8%8B%E6%8A%8A%E8%BF%99%E4%BA%9B%E8%AE%A1%E7%AE%97%E9%83%BD%E6%89%A7%E8%A1%8C%E5%AE%8C%E6%AF%95%E4%B9%8B%E5%90%8E%E5%B0%B1%E6%8A%8A%E7%BB%93%E6%9E%9C%E5%9B%9E%E8%B0%83%E5%88%B0%E6%88%91%E4%BB%AC%E7%9A%84node-js%E7%BA%BF%E7%A8%8B-node-js%E5%86%8D%E6%8A%8A%E7%BA%BF%E7%A8%8B%E6%8A%8A%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%9C%E8%BF%94%E5%9B%9E%E7%BB%99%E6%88%91%E4%BB%AC%E7%9A%84%E5%BA%94%E7%94%A8%E7%A8%8B%E5%BA%8F"><span class="toc-number">5.1.</span> <span class="toc-text">Node.js的所有I&#x2F;O操作都是非阻塞的,会把大量的计算能力分发给其他c++线程计算等到其他c++线程把这些计算都执行完毕之后就把结果回调到我们的node.js线程,node.js再把线程把数据结果返回给我们的应用程序</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#Eventloop%E4%BA%8B%E4%BB%B6%E5%BE%AA%E7%8E%AF"><span class="toc-number">6.</span> <span class="toc-text">Eventloop事件循环</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E8%AF%B4%E6%98%8E%E6%B7%BB%E5%8A%A0%E5%88%B0%E4%BA%8B%E4%BB%B6%E5%90%8E%E4%BC%9A%E5%86%8D%E6%AC%A1%E6%89%A7%E8%A1%8Ceventloop-%E4%BA%8B%E4%BB%B6%E5%BE%AA%E7%8E%AF"><span class="toc-number">6.1.</span> <span class="toc-text">说明添加到事件后会再次执行eventloop 事件循环</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#Promise"><span class="toc-number">7.</span> <span class="toc-text">Promise</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#async%E3%80%81await"><span class="toc-number">8.</span> <span class="toc-text">async、await</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%BC%82%E6%AD%A5%E7%BC%96%E7%A8%8B%E7%9A%84%E7%BB%88%E6%9E%81%E6%96%B9%E6%A1%88%E2%80%94%E4%BB%A5%E5%90%8C%E6%AD%A5%E7%9A%84%E6%96%B9%E5%BC%8F%E5%86%99%E5%BC%82%E6%AD%A5"><span class="toc-number">8.1.</span> <span class="toc-text">异步编程的终极方案—以同步的方式写异步</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E7%AE%80%E5%8D%95%E7%9A%84http%E6%9C%8D%E5%8A%A1"><span class="toc-number">9.</span> <span class="toc-text">简单的http服务</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E7%BD%91%E9%A1%B5%E7%89%88%E7%9F%B3%E5%A4%B4%E5%89%AA%E5%88%80%E5%B8%83%E6%B8%B8%E6%88%8F"><span class="toc-number">10.</span> <span class="toc-text">网页版石头剪刀布游戏</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#express%E7%9A%84%E5%8A%9F%E8%83%BD%E4%B8%8E%E7%AE%80%E5%8D%95%E4%BD%BF%E7%94%A8"><span class="toc-number">11.</span> <span class="toc-text">express的功能与简单使用</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#express%E7%9A%84%E5%8A%9F%E8%83%BD%E7%89%B9%E7%82%B9"><span class="toc-number">11.1.</span> <span class="toc-text">express的功能特点:</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%83%8Fexpress%E8%BF%99%E7%A7%8D%E8%B0%83%E7%94%A8%E5%BC%82%E6%AD%A5%E7%9A%84%E6%B4%8B%E8%91%B1%E6%A8%A1%E5%9E%8B%E5%AD%98%E5%9C%A8%E5%A4%84%E7%90%86%E7%BC%BA%E9%99%B7%E2%80%94%E2%80%93%E5%AF%B9%E5%BC%82%E6%AD%A5%E7%9A%84%E6%94%AF%E6%8C%81%E4%B8%8D%E5%A4%AA%E5%A5%BD%E2%80%94%E2%80%94%E2%80%94%E5%87%BA%E7%8E%B0%E4%BA%86koa%E6%A1%86%E6%9E%B6%E8%A7%A3%E5%86%B3"><span class="toc-number">11.2.</span> <span class="toc-text">像express这种调用异步的洋葱模型存在处理缺陷—–对异步的支持不太好———出现了koa框架解决</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#express%E7%9A%84%E4%BD%BF%E7%94%A8"><span class="toc-number">11.3.</span> <span class="toc-text">express的使用</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#Koa%E7%9A%84%E5%8A%9F%E8%83%BD%E4%B8%8E%E7%AE%80%E5%8D%95%E4%BD%BF%E7%94%A8"><span class="toc-number">12.</span> <span class="toc-text">Koa的功能与简单使用</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#Koa%E7%9A%84%E6%A0%B8%E5%BF%83%E5%8A%9F%E8%83%BD"><span class="toc-number">12.1.</span> <span class="toc-text">Koa的核心功能</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#Express%E7%9A%84%E6%A0%B8%E5%BF%83%E5%8A%9F%E8%83%BD"><span class="toc-number">12.2.</span> <span class="toc-text">Express的核心功能</span></a></li></ol></li></ol></div></div><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>最新文章</span></div><div class="aside-list"><div class="aside-list-item"><a class="thumbnail" href="/blog/posts/1/" title="无题"><img src="https://gitee.com/mo_zi_hao/picsBed/raw/master/image/%E6%B5%B7%E8%B4%BC4.jpg" onerror="this.onerror=null;this.src='/blog/img/404.jpg'" alt="无题"/></a><div class="content"><a class="title" href="/blog/posts/1/" title="无题">无题</a><time datetime="2022-06-05T05:20:17.974Z" title="发表于 2022-06-05 13:20:17">2022-06-05</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/blog/posts/36549/" title="编写可维护的JavaScript (代码习惯)"><img src="https://gitee.com/mo_zi_hao/picsBed/raw/master/image/%E6%B5%B7%E8%B4%BC4.jpg" onerror="this.onerror=null;this.src='/blog/img/404.jpg'" alt="编写可维护的JavaScript (代码习惯)"/></a><div class="content"><a class="title" href="/blog/posts/36549/" title="编写可维护的JavaScript (代码习惯)">编写可维护的JavaScript (代码习惯)</a><time datetime="2022-03-19T03:12:42.624Z" title="发表于 2022-03-19 11:12:42">2022-03-19</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/blog/posts/23125/" title="gitlab-cicd"><img src="https://gitee.com/mo_zi_hao/picsBed/raw/master/image/745.jpg" onerror="this.onerror=null;this.src='/blog/img/404.jpg'" alt="gitlab-cicd"/></a><div class="content"><a class="title" href="/blog/posts/23125/" title="gitlab-cicd">gitlab-cicd</a><time datetime="2021-12-20T14:00:59.661Z" title="发表于 2021-12-20 22:00:59">2021-12-20</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/blog/posts/449/" title="前端js基础"><img src="https://gitee.com/mo_zi_hao/picsBed/raw/master/image/26&amp;gp=0.jpg" onerror="this.onerror=null;this.src='/blog/img/404.jpg'" alt="前端js基础"/></a><div class="content"><a class="title" href="/blog/posts/449/" title="前端js基础">前端js基础</a><time datetime="2021-08-29T15:22:14.600Z" title="发表于 2021-08-29 23:22:14">2021-08-29</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/blog/posts/35506/" title="微信小程序"><img src="https://gitee.com/mo_zi_hao/picsBed/raw/master/image/1234.jpg" onerror="this.onerror=null;this.src='/blog/img/404.jpg'" alt="微信小程序"/></a><div class="content"><a class="title" href="/blog/posts/35506/" title="微信小程序">微信小程序</a><time datetime="2021-08-29T15:22:14.600Z" title="发表于 2021-08-29 23:22:14">2021-08-29</time></div></div></div></div></div></div></main><footer id="footer"><div id="footer-wrap"><div class="copyright">&copy;2020 - 2023 By mozihao</div><div class="framework-info"><span>框架 </span><a target="_blank" rel="noopener" href="https://hexo.io">Hexo</a><span class="footer-separator">|</span><span>主题 </span><a target="_blank" rel="noopener" href="https://github.com/jerryc127/hexo-theme-butterfly">Butterfly</a></div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="translateLink" type="button" title="简繁转换">繁</button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button" title="单栏和双栏切换"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="目录"><i class="fas fa-list-ul"></i></button><button id="go-up" type="button" title="回到顶部"><i class="fas fa-arrow-up"></i></button></div></div><div id="local-search"><div class="search-dialog"><div class="search-dialog__title" id="local-search-title">本地搜索</div><div id="local-input-panel"><div id="local-search-input"><div class="local-search-box"><input class="local-search-box--input" placeholder="搜索文章" type="text"/></div></div></div><hr/><div id="local-search-results"></div><span class="search-close-button"><i class="fas fa-times"></i></span></div><div id="search-mask"></div></div><div><script src="/blog/js/utils.js"></script><script src="/blog/js/main.js"></script><script src="/blog/js/tw_cn.js"></script><script src="https://cdn.jsdelivr.net/npm/vanilla-lazyload/dist/lazyload.iife.min.js"></script><script src="/blog/js/search/local-search.js"></script><div class="js-pjax"></div><script id="click-heart" src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/dist/click-heart.min.js" async="async" mobile="false"></script><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/aplayer/dist/APlayer.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/npm/aplayer/dist/APlayer.min.js"></script><script src="https://cdn.jsdelivr.net/gh/metowolf/MetingJS@1.2/dist/Meting.min.js"></script><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script></div></body></html>